/* Copyright (c) 2014, Yahoo! Inc. All rights reserved. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ /* jslint esnext: true */ var round = Math.round; function daysToYears(days) { // 400 years have 146097 days (taking into account leap year rules) return days * 400 / 146097; } // Thanks to date-fns // https://github.com/date-fns/date-fns // MIT © Sasha Koss var MILLISECONDS_IN_MINUTE = 60000; var MILLISECONDS_IN_DAY = 86400000; function startOfDay (dirtyDate) { var date = new Date(dirtyDate); date.setHours(0, 0, 0, 0); return date; } function differenceInCalendarDays (dirtyDateLeft, dirtyDateRight) { var startOfDayLeft = startOfDay(dirtyDateLeft); var startOfDayRight = startOfDay(dirtyDateRight); var timestampLeft = startOfDayLeft.getTime() - startOfDayLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE; var timestampRight = startOfDayRight.getTime() - startOfDayRight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE; // Round the number of days to the nearest integer // because the number of milliseconds in a day is not constant // (e.g. it's different in the day of the daylight saving time clock shift) return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_DAY); } export default function (from, to) { // Convert to ms timestamps. from = +from; to = +to; var millisecond = round(to - from), second = round(millisecond / 1000), minute = round(second / 60), hour = round(minute / 60); // We expect a more precision in rounding when dealing with // days as it feels wrong when something happended 13 hours ago and // is regarded as "yesterday" even if the time was this morning. var day = differenceInCalendarDays(to, from); var week = round(day / 7); var rawYears = daysToYears(day), month = round(rawYears * 12), year = round(rawYears); return { millisecond : millisecond, second : second, 'second-short' : second, minute : minute, 'minute-short' : minute, hour : hour, 'hour-short' : hour, day : day, 'day-short' : day, week : week, 'week-short' : week, month : month, 'month-short' : month, year : year, 'year-short' : year }; }