-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.util.ts
181 lines (153 loc) · 5.82 KB
/
date.util.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright 2019 Next Century Corporation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as moment from 'moment';
export enum DateFormat {
DAY = 'MMM D, YYYY',
ISO = 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]',
MINUTE = 'MMM D, YYYY, h:mm A',
MONTH = 'MMM YYYY',
PRETTY = 'ddd, MMM D, YYYY, h:mm A',
SHORT = 'YYYY-MM-DD',
YEAR = 'YYYY'
}
export class DateUtil {
static STANDARD_FORMAT: DateFormat = DateFormat.ISO;
static USE_LOCAL_TIME: boolean = false;
/**
* Add one of the given interval (minute/hour/day/month/year) to the given date object, subtract one second, and
* return the new date.
*/
static addOneOfIntervalToDate(input: Date, interval: string): Date {
let momentObject = moment.utc(input);
momentObject.add(1, interval as moment.unitOfTime.DurationConstructor);
momentObject.subtract(1, 'second');
return momentObject.toDate();
}
/**
* Returns the date string for the given date object (or a date string formatted using the standard date format)
* formatted using the given output date format (or the standard date format if no date format is given).
*/
static fromDateToString(input: Date|string, outputFormat: DateFormat = DateUtil.STANDARD_FORMAT): string {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().format(outputFormat as string);
}
return DateUtil.fromUnknownInputToMoment(input).format(outputFormat as string);
}
/**
* Returns the date object for the given formatted date string.
*/
static fromStringToDate(input: string): Date {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().toDate();
}
return DateUtil.fromUnknownInputToMoment(input).toDate();
}
/**
* Returns the day of month (integer) for the given formatted date string.
*/
static fromStringToDay(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().date();
}
return DateUtil.fromUnknownInputToMoment(input).date();
}
/**
* Returns the hours (integer) for the given formatted date string.
*/
static fromStringToHour(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().hour();
}
return DateUtil.fromUnknownInputToMoment(input).hour();
}
/**
* Returns the minutes (integer) for the given formatted date string.
*/
static fromStringToMinute(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().minute();
}
return DateUtil.fromUnknownInputToMoment(input).minute();
}
/**
* Returns the zero-indexed month (integer) for the given formatted date string.
*/
static fromStringToMonth(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().month();
}
return DateUtil.fromUnknownInputToMoment(input).month();
}
/**
* Returns the seconds (integer) for the given formatted date string.
*/
static fromStringToSecond(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().second();
}
return DateUtil.fromUnknownInputToMoment(input).second();
}
/**
* Returns the unix timestamp in milliseconds for the given formatted date string.
*/
static fromStringToTimestamp(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().valueOf();
}
return DateUtil.fromUnknownInputToMoment(input).valueOf();
}
/**
* Returns the year (integer) for the given formatted date string.
*/
static fromStringToYear(input: string): number {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().year();
}
return DateUtil.fromUnknownInputToMoment(input).year();
}
/**
* Returns the moment object for the given input.
*/
static fromUnknownInputToMoment(input: any): any {
if (isNaN(input)) {
return moment.utc(input, DateUtil.STANDARD_FORMAT as string);
}
return moment.utc(input);
}
/**
* Returns the date string for the given date object as "X seconds/minutes/hours/days ago".
*/
static retrievePastTime(input: Date|string, outputFormat?: DateFormat): string {
if (this.USE_LOCAL_TIME) {
if (moment.parseZone(input).local().diff(Date.now(), 'd', true) < -3) {
return DateUtil.fromDateToString(input, outputFormat);
}
return moment.parseZone(input).local().fromNow();
}
if (DateUtil.fromUnknownInputToMoment(input).diff(Date.now(), 'd', true) < -3) {
return DateUtil.fromDateToString(input, outputFormat);
}
return DateUtil.fromUnknownInputToMoment(input).fromNow();
}
/**
* Returns if the given date string is a valid date.
*/
static verifyDateStringStrict(input: string): boolean {
if (this.USE_LOCAL_TIME) {
return moment.parseZone(input).local().isValid();
}
return moment.utc(input, DateUtil.STANDARD_FORMAT as string, true).isValid();
}
}