deno.land / std@0.224.0 / datetime / difference_test.ts

difference_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals } from "../assert/mod.ts";import { difference } from "./difference.ts";
Deno.test({ name: "difference()", fn() { const denoInit = new Date("2018/5/14"); const denoReleaseV1 = new Date("2020/5/13");
// The order of the dates does not matter assertEquals( difference(denoInit, denoReleaseV1), difference(denoReleaseV1, denoInit), );
let diff = difference(denoReleaseV1, denoInit, { units: ["days", "weeks", "months", "years", "quarters"], }); assertEquals(diff.days, 730); assertEquals(diff.weeks, 104); assertEquals(diff.months, 23); assertEquals(diff.quarters, 7); assertEquals(diff.years, 1);
// Default units diff = difference(denoReleaseV1, denoInit); assertEquals(diff.days, 730); assertEquals(diff.weeks, 104); assertEquals(diff.months, 23); assertEquals(diff.quarters, 7); assertEquals(diff.years, 1);
// If `options.units.months` isn't defined diff = difference(denoReleaseV1, denoInit, { units: ["years", "quarters"], }); assertEquals(diff.quarters, 7); assertEquals(diff.years, 1);
const birth = new Date("1998/2/23 10:10:10"); const old = new Date("1998/2/23 11:11:11"); diff = difference(birth, old, { units: ["milliseconds", "minutes", "seconds", "hours"], }); assertEquals(diff.milliseconds, 3661000); assertEquals(diff.seconds, 3661); assertEquals(diff.minutes, 61); assertEquals(diff.hours, 1); },});
std

Version Info

Tagged at
6 months ago