-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.service.js
152 lines (135 loc) · 4.26 KB
/
date.service.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
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
'use strict'
const moment = require('moment')
const gdsFullFormat = 'D MMMM YYYY'
const gdsShortFormat = 'D MMM YYYY'
const UKFormat = 'DD/MM/YYYY'
const yearFormat = 'YYYY'
const reverseFormatNoSeparator = 'YYYYMMDD'
const timeFormatWithSeconds = 'h:mm:ss a'
const dayAndDateFormat = 'dddd, D MMMM'
const dayDateAndYearFormat = 'dddd D MMMM YYYY'
const dateAndTimeFormat = 'D MMMM YYYY h:mma'
const iso8601WithMsPrecisionAndTimeZone = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
const filenameFriendly = 'YYYY-MM-DD-HHmm'
const dateService = {
formatYear: function (date) {
return moment(date).format(yearFormat)
},
formatFullGdsDate: function (date) {
return dateService.checkAndFormat(date, gdsFullFormat)
},
formatShortGdsDate: function (date) {
return dateService.checkAndFormat(date, gdsShortFormat)
},
formatDayAndDate: function (date) {
return moment(date).format(dayAndDateFormat)
},
formatDayDateAndYear: function (date) {
return moment(date).format(dayDateAndYearFormat)
},
formatDateAndTime: function (date) {
return moment(date).format(dateAndTimeFormat)
},
formatUKDate: function (date) {
return dateService.checkAndFormat(date, UKFormat)
},
reverseFormatNoSeparator: function (date) {
return dateService.checkAndFormat(date, reverseFormatNoSeparator)
},
formatTimeWithSeconds: function (date) {
return dateService.checkAndFormat(date, timeFormatWithSeconds)
},
formatIso8601: function (momentDate) {
if (!moment.isMoment(momentDate)) {
throw new Error('Parameter must be of type Moment')
}
if (!momentDate.isValid()) {
throw new Error('Not a valid date')
}
return momentDate.format(iso8601WithMsPrecisionAndTimeZone)
},
checkAndFormat: function (date, format) {
if (!(date instanceof Date || moment.isMoment(date))) {
return ''
}
const m = moment(date)
if (!m.isValid()) {
return ''
}
return m.format(format)
},
/**
* Format check windows dates.
* @param dateItem
* @param keyDay
* @param keyMonth
* @param keyYear
* @returns {Moment}
*/
formatDateFromRequest: function (dateItem, keyDay, keyMonth, keyYear) {
return moment.utc(
'' + dateItem[keyDay] +
'/' + dateItem[keyMonth] +
'/' + dateItem[keyYear],
'D/MM/YYYY')
},
/**
* Format period (start and end dates)
* @param startDate
* @param endDate
* @returns {string} E.g. "1 Nov to 20 Nov 2017" or "1 Dec 2016 to 1 Jan 2017"
*/
formatCheckPeriod: function (startDate, endDate) {
let startYear = ' ' + startDate.format('YYYY')
let endYear = ' ' + endDate.format('YYYY')
if (startYear === endYear) {
startYear = ''
}
return startDate.format('D MMM') + startYear + ' to ' + endDate.format('D MMM YYYY')
},
/**
* Return a moment object from the a day, month and year. The time component will be zeroed out. Returns null if invalid.
* @param {number|string} day
* @param {number|string} month
* @param {number|string} year
* @returns {Moment}
*/
createLocalTimeFromDayMonthYear: function (day, month, year) {
const paddedDay = (+day).toString().padStart(2, '0')
const paddedMonth = (+month).toString().padStart(2, '0')
const data = paddedDay + '/' + paddedMonth + '/' + (+year).toString()
const date = moment(data, 'DD/MM/YYYY', true)
if (!date.isValid()) {
return null
}
return date
},
/**
* Return a UTC-mode moment object from the a day, month and year. The time component will be zeroed out. Returns null if invalid.
* @param {number|string} day
* @param {number|string} month
* @param {number|string} year
* @returns {Moment}
*/
createUTCFromDayMonthYear: function (day, month, year) {
const paddedDay = (+day).toString().padStart(2, '0')
const paddedMonth = (+month).toString().padStart(2, '0')
const data = paddedDay + '/' + paddedMonth + '/' + (+year).toString()
const date = moment.utc(data, 'DD/MM/YYYY', true)
if (!date.isValid()) {
return null
}
return date
},
/**
* returns current UTC date and time as moment
* @returns {Moment}
*/
utcNowAsMoment: () => {
return moment.utc()
},
formatFileName: function (date) {
return moment(date).format(filenameFriendly)
}
}
module.exports = dateService