diff options
Diffstat (limited to 'date-fns/src/differenceInMilliseconds')
-rw-r--r-- | date-fns/src/differenceInMilliseconds/benchmark.js | 23 | ||||
-rw-r--r-- | date-fns/src/differenceInMilliseconds/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/differenceInMilliseconds/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/differenceInMilliseconds/index.ts | 39 | ||||
-rw-r--r-- | date-fns/src/differenceInMilliseconds/test.ts | 70 |
5 files changed, 191 insertions, 0 deletions
diff --git a/date-fns/src/differenceInMilliseconds/benchmark.js b/date-fns/src/differenceInMilliseconds/benchmark.js new file mode 100644 index 0000000..ab4d05a --- /dev/null +++ b/date-fns/src/differenceInMilliseconds/benchmark.js @@ -0,0 +1,23 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import differenceInMilliseconds from '.' +import moment from 'moment' + +suite('differenceInMilliseconds', function () { + benchmark('date-fns', function () { + return differenceInMilliseconds(this.dateA, this.dateB) + }) + + benchmark('Moment.js', function () { + return this.momentA.diff(this.momentB) + }) +}, { + setup: function () { + this.dateA = new Date() + this.momentA = moment() + this.dateB = new Date(this.dateA.getTime() + 604800000) + this.momentB = this.momentA.clone().add(7, 'days') + } +}) diff --git a/date-fns/src/differenceInMilliseconds/index.d.ts b/date-fns/src/differenceInMilliseconds/index.d.ts new file mode 100644 index 0000000..c72a3ab --- /dev/null +++ b/date-fns/src/differenceInMilliseconds/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { differenceInMilliseconds } from 'date-fns' +export default differenceInMilliseconds diff --git a/date-fns/src/differenceInMilliseconds/index.js.flow b/date-fns/src/differenceInMilliseconds/index.js.flow new file mode 100644 index 0000000..c799819 --- /dev/null +++ b/date-fns/src/differenceInMilliseconds/index.js.flow @@ -0,0 +1,55 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: ( + dateLeft: Date | number, + dateRight: Date | number +) => number diff --git a/date-fns/src/differenceInMilliseconds/index.ts b/date-fns/src/differenceInMilliseconds/index.ts new file mode 100644 index 0000000..a0fc552 --- /dev/null +++ b/date-fns/src/differenceInMilliseconds/index.ts @@ -0,0 +1,39 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name differenceInMilliseconds + * @category Millisecond Helpers + * @summary Get the number of milliseconds between the given dates. + * + * @description + * Get the number of milliseconds between the given dates. + * + * ### v2.0.0 breaking changes: + * + * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). + * + * @param {Date|Number} dateLeft - the later date + * @param {Date|Number} dateRight - the earlier date + * @returns {Number} the number of milliseconds + * @throws {TypeError} 2 arguments required + * + * @example + * // How many milliseconds are between + * // 2 July 2014 12:30:20.600 and 2 July 2014 12:30:21.700? + * const result = differenceInMilliseconds( + * new Date(2014, 6, 2, 12, 30, 21, 700), + * new Date(2014, 6, 2, 12, 30, 20, 600) + * ) + * //=> 1100 + */ +export default function differenceInMilliseconds( + dirtyDateLeft: Date | number, + dirtyDateRight: Date | number +): number { + requiredArgs(2, arguments) + + const dateLeft = toDate(dirtyDateLeft) + const dateRight = toDate(dirtyDateRight) + return dateLeft.getTime() - dateRight.getTime() +} diff --git a/date-fns/src/differenceInMilliseconds/test.ts b/date-fns/src/differenceInMilliseconds/test.ts new file mode 100644 index 0000000..366c03b --- /dev/null +++ b/date-fns/src/differenceInMilliseconds/test.ts @@ -0,0 +1,70 @@ +/* eslint-env mocha */ + +import assert from 'power-assert' +import differenceInMilliseconds from '.' + +describe('differenceInMilliseconds', function () { + it('returns the number of milliseconds between the given dates', function () { + const result = differenceInMilliseconds( + new Date(2014, 6 /* Jul */, 2, 12, 30, 20, 700), + new Date(2014, 6 /* Jul */, 2, 12, 30, 20, 600) + ) + assert(result === 100) + }) + + it('returns a negative number if the time value of the first date is smaller', function () { + const result = differenceInMilliseconds( + new Date(2014, 6 /* Jul */, 2, 12, 30, 20, 600), + new Date(2014, 6 /* Jul */, 2, 12, 30, 20, 700) + ) + assert(result === -100) + }) + + it('accepts timestamps', function () { + const result = differenceInMilliseconds( + new Date(2014, 8 /* Sep */, 5, 18, 30, 45, 500).getTime(), + new Date(2014, 8 /* Sep */, 5, 18, 30, 45, 500).getTime() + ) + assert(result === 0) + }) + + it('does not return -0 when the given dates are the same', () => { + function isNegativeZero(x: number): boolean { + return x === 0 && 1 / x < 0 + } + + const result = differenceInMilliseconds( + new Date(2014, 8 /* Sep */, 5, 0, 0), + new Date(2014, 8 /* Sep */, 5, 0, 0) + ) + + const resultIsNegative = isNegativeZero(result) + assert(resultIsNegative === false) + }) + + it('returns NaN if the first date is `Invalid Date`', function () { + const result = differenceInMilliseconds( + new Date(NaN), + new Date(2017, 0 /* Jan */, 1) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the second date is `Invalid Date`', function () { + const result = differenceInMilliseconds( + new Date(2017, 0 /* Jan */, 1), + new Date(NaN) + ) + assert(isNaN(result)) + }) + + it('returns NaN if the both dates are `Invalid Date`', function () { + const result = differenceInMilliseconds(new Date(NaN), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function () { + assert.throws(differenceInMilliseconds.bind(null), TypeError) + assert.throws(differenceInMilliseconds.bind(null, 1), TypeError) + }) +}) |