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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
//
// date.ts
// AppStoreKit
//
// Created by Sam Vafaee on 10/25/17.
// Copyright (c) 2017 Apple Inc. All rights reserved.
//
import * as serverData from "../json-parsing/server-data";
/**
* Returns the date representation for a string ignoring the time. For preorders, it
* is vital that any date string be passed through this before being used.
* @param dateString The date string to parse
* @returns A date in local time zone.
*/
export function parseDateOmittingTimeFromString(dateString) {
// Sanity check
if (serverData.isNull(dateString) || dateString === "") {
return null;
}
// Only consider date
if (dateString.indexOf("T") !== -1) {
dateString = dateString.split("T")[0];
}
// This string replacement is needed to ensure midnight in the local time zone
// is used, rather than UTC-midnight.
return new Date(dateString.replace(/-/g, "/"));
}
/**
* Strips time from the given date and returns it as midnight UTC of the same day.
* @param date Local date.
* @returns UTC date
*/
export function convertLocalDateToUTCMidnight(date) {
// Sanity check
if (serverData.isNull(date)) {
return null;
}
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
}
/**
* Returns the number of milliseconds from epoch to UTC midnight of the given date.
* @param date Local date.
* @returns Milliseconds from epoch.
*/
export function millisecondsToUTCMidnightFromLocalDate(date) {
// Sanity check
if (serverData.isNull(date)) {
return null;
}
const utcDate = convertLocalDateToUTCMidnight(date);
if (serverData.isNull(utcDate)) {
return null;
}
return utcDate.getTime();
}
/**
* Removes the time portion from the given date and returns it as midnight of the same day.
* @param date Local date
* @returns Local date
*/
export function convertLocalDateToLocalMidnight(date) {
// Sanity check
if (serverData.isNull(date)) {
return null;
}
const midnight = new Date(date);
midnight.setHours(0, 0, 0, 0);
return midnight;
}
/**
* Sets the minutes, seconds and milliseconds of the given date to 0.
* @param date Local date
* @returns Local date
*/
export function convertLocalDateByFlooringToHour(date) {
// Sanity check
if (serverData.isNull(date)) {
return null;
}
return dateFlooredToHour(date);
}
/**
* Creates a new date with the minutes, seconds and milliseconds of the given date set to 0.
* @param date The date to remove minutes, seconds, and milliseconds from.
* @returns The new date with minutes, seconds, and milliseconds removed using local device time zone.
*/
export function dateFlooredToHour(date) {
const dateCopy = new Date(date.getTime());
dateCopy.setMinutes(0);
dateCopy.setSeconds(0);
dateCopy.setMilliseconds(0);
return dateCopy;
}
/**
* Returns the number of hours between two dates, or null if either date is null.
* @param fromDate The date to calculate from.
* @param toDate The date to calculate to.
*/
export function numberOfHoursBetween(fromDate, toDate) {
// Sanity check
if (serverData.isNull(fromDate) || serverData.isNull(toDate)) {
return null;
}
return Math.ceil((toDate.getTime() - dateFlooredToHour(fromDate).getTime()) / (60 * 60 * 1000));
}
/**
* Returns the number of days between two dates, or null if either date is null.
* @param fromDate The date to calculate from.
* @param toDate The date to calculate to.
*/
export function numberOfDaysBetween(fromDate, toDate) {
// Sanity check
if (serverData.isNull(fromDate) || serverData.isNull(toDate)) {
return null;
}
return Math.floor((toDate.getTime() - fromDate.getTime()) / (60 * 60 * 1000 * 24));
}
/**
* Whether or not two dates occur on the same day.
* @param date A date that will be compared to the second input.
* @param otherDate A date that will be compared to the first input.
*/
export function areLocalDatesSameDay(date, otherDate) {
var _a, _b;
return ((_a = convertLocalDateToLocalMidnight(date)) === null || _a === void 0 ? void 0 : _a.getTime()) === ((_b = convertLocalDateToLocalMidnight(otherDate)) === null || _b === void 0 ? void 0 : _b.getTime());
}
//# sourceMappingURL=date-util.js.map
|