-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1154-day-of-the-year.js
52 lines (37 loc) · 1.4 KB
/
1154-day-of-the-year.js
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
// 1154. Day of the Year
// https://leetcode.com/problems/day-of-the-year/
/*
Given a string date representing a Gregorian calendar date formatted as
YYYY-MM-DD, return the day number of the year.
*/
import { strictEqual } from 'assert';
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 52 ms, faster than 89.84% of JavaScript online submissions
// Memory Usage: 34.5 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {string} date
// * @return {number}
// */
// const dayOfYear = date =>
// (Date.parse(date) - Date.parse(`${new Date(date).getFullYear()}-01-01`)) /
// 86400000 +
// 1;
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 60 ms, faster than 58.59% of JavaScript online submissions
// Memory Usage: 33.9 MB, less than 100.00% of JavaScript online submissions
/**
* @param {string} date
* @return {number}
*/
const dayOfYear = date =>
(Date.parse(date) - Date.parse(`${date.slice(0, 4)}-01-01`)) / 86400000 + 1;
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Example 1:
strictEqual(dayOfYear('2019-01-09'), 9);
// Explanation: Given date is the 9th day of the year in 2019.
// Example 2:
strictEqual(dayOfYear('2019-02-10'), 41);
// Example 3:
strictEqual(dayOfYear('2003-03-01'), 60);
// Example 4:
strictEqual(dayOfYear('2004-03-01'), 61);