-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (73 loc) · 1.86 KB
/
index.js
File metadata and controls
80 lines (73 loc) · 1.86 KB
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
const GMT = require('node-gmt')
/**
* Retrieve a 0 before the first digit when the number is less than 10
* @param {number} number
* @returns {String} string
*/
const addCero = (num) => {
return num < 10 ? `0${num}` : num
}
const gmtChecker = (gmt) => {
const array = [
'GMT+00:00',
'GMT-00:00',
'GMT+01:00',
'GMT+02:00',
'GMT+02:00',
'GMT+03:00',
'GMT+03:30',
'GMT+04:00',
'GMT+05:00',
'GMT+05:30',
'GMT+06:00',
'GMT+07:00',
'GMT+08:00',
'GMT+09:00',
'GMT+09:30',
'GMT+10:00',
'GMT+11:00',
'GMT+12:00',
'GMT-11:00',
'GMT-10:00',
'GMT-09:00',
'GMT-08:00',
'GMT-07:00',
'GMT-07:00',
'GMT-06:00',
'GMT-05:00',
'GMT-05:00',
'GMT-04:00',
'GMT-03:30',
'GMT-03:00',
'GMT-03:00',
'GMT-01:00'
]
if (gmt === undefined) return true
return array.includes(gmt)
}
/**
* Retrieve the date as format yyyy/mm/dd hh:mm:ss
* @param {timestamp} date
* @param {gmt hours} string
* @returns {String} string
*/
module.exports = (dateGetThem, gmtHours) => {
if (dateGetThem !== undefined || dateGetThem) {
if (typeof dateGetThem === 'number') return -1
const valid = (new Date(dateGetThem)).getTime() > 0;
if (!valid) return -1
}
if (!gmtChecker(gmtHours)) return -1
gmtHours = !gmtHours ? 'GMT+00:00' : gmtHours
const gmt = new GMT(gmtHours)
let currentDate = (!dateGetThem) ? new Date() : new Date(dateGetThem)
currentDate = gmt.relativeDate(currentDate)
const date = currentDate.getDate()
const month = currentDate.getMonth()
const year = currentDate.getFullYear()
const hour = currentDate.getHours()
const minutes = currentDate.getMinutes()
const seconds = currentDate.getSeconds()
const yearMonthDay = `${year}/${addCero(month + 1)}/${addCero(date)} ${addCero(hour)}:${addCero(minutes)}:${addCero(seconds)}`
return yearMonthDay
}