-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdateType.js
More file actions
73 lines (66 loc) · 1.95 KB
/
dateType.js
File metadata and controls
73 lines (66 loc) · 1.95 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
let date = new Date(2015, 0, 2)
console.log(date)
function getWeekDay(date) {
let weekDay = [
'ВС', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ'
]
return weekDay[date.getDay()]
}
function getLocalyDay(date) {
let localyDay = date.getDay()
if (localyDay == 0) {
localyDay = 7
}
return localyDay;
}
function getDayAgo(date, days) {
let dateAgo = new Date(date)
dateAgo.setDate(date.getDate() - days)
return dateAgo.getDate()
}
function getLastDayOfMonth(year, month) {
let date = new Date(year, month, 0)
return date.getDate()
}
function getSecondsToday() {
const today = new Date()
return today.getHours() * 3600 + today.getMinutes() * 60 + today.getSeconds()
}
function getSecondsToTomorrow() {
const now = new Date()
const totalSeondsInADay = 86400
return totalSeondsInADay - (now.getHours() * 60 + now.getMinutes()) * 60 + now.getSeconds()
}
function formatDate(date) {
const diff = new Date() - date
if (diff < 1000) {
return 'Прямо сейчас'
}
let sec = Math.floor(diff / 1000)
if (sec < 60) {
return sec + ' сек назад'
}
let min = Math.floor(diff / 60000)
if (min < 60) {
return min + ' мин назад'
}
let d = date
d = [
'0' + d.getDate(),
'0' + (d.getMonth() + 1),
'' + d.getFullYear(),
'0' + d.getHours(),
'0' + d.getMinutes()
].map(component => component.slice(-2))
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':')
}
console.log(getLocalyDay(date))
console.log(getWeekDay(date))
console.log(getDayAgo(date, 3))
console.log(getLastDayOfMonth(2013, 2))
console.log(getSecondsToday())
console.log(getSecondsToTomorrow())
console.log(formatDate(new Date(new Date - 1)))
console.log(formatDate(new Date(new Date - 2 * 1000)))
console.log(formatDate(new Date(new Date - 5 * 60 * 1000)))
console.log(formatDate(new Date(new Date - 86400 * 1000)))