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

is_leap.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
function isYearNumberALeapYear(yearNumber: number): boolean { return ( (yearNumber % 4 === 0 && yearNumber % 100 !== 0) || yearNumber % 400 === 0 );}
/** * Returns whether the given year is a leap year. Passing in a * {@linkcode Date} object will return the leap year status of the year of that * object and take the current timezone into account. Passing in a number will * return the leap year status of that number. * * This is based on * {@link https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year}. * * @param year The year in number or `Date` format. * @returns `true` if the given year is a leap year; `false` otherwise. * * @example Basic usage * ```ts * import { isLeap } from "https://deno.land/std@$STD_VERSION/datetime/is_leap.ts"; * * isLeap(new Date("1970-01-02")); // false * * isLeap(1970); // false * * isLeap(new Date("1972-01-02")); // true * * isLeap(1972); // true * ``` * * @example Accounting for timezones * ```ts * import { isLeap } from "https://deno.land/std@$STD_VERSION/datetime/is_leap.ts"; * * isLeap(new Date("2000-01-01")); * // true if the local timezone is GMT+0; false if the local timezone is GMT-1 * * isLeap(2000); * // true regardless of the local timezone * ``` */export function isLeap(year: Date | number): boolean { const yearNumber = year instanceof Date ? year.getFullYear() : year; return isYearNumberALeapYear(yearNumber);}
/** * Returns whether the given year is a leap year in UTC time. This always * returns the same value regardless of the local timezone.
* This is based on * {@link https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year}. * * @param year The year in number or `Date` format. * @returns `true` if the given year is a leap year; `false` otherwise. * * @example Basic usage * ```ts * import { isUtcLeap } from "https://deno.land/std@$STD_VERSION/datetime/is_leap.ts"; * * isUtcLeap(new Date("2000-01-01")); // true * * isUtcLeap(new Date("December 31, 1999 23:59:59 GMT-01:00")); // true * * isUtcLeap(2000); // true * * isUtcLeap(1999); // false * ``` */export function isUtcLeap(year: Date | number): boolean { const yearNumber = year instanceof Date ? year.getUTCFullYear() : year; return isYearNumberALeapYear(yearNumber);}
std

Version Info

Tagged at
6 months ago