-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatDate.js
56 lines (41 loc) · 1.29 KB
/
formatDate.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
function formatDate(date) {
var diff = new Date() - date;
if (diff < 1000) {
return "только что";
}
var sec = Math.floor(diff / 1000);
if (sec < 60) {
return sec + " сек. назад";
}
var min = Math.floor(diff / 60000);
if (min < 60) {
return min + " мин. назад"
}
var d = date;
d = [
"0" + d.getDate(),
"0" + (d.getMonth() + 1),
"" + d.getFullYear(),
"0" + d.getHours(),
"0" + d.getMinutes()
];
for (var i = 0; i < d.length; i++) {
d[i] = d[i].slice(-2);
}
return d.slice(0, 3).join(".") + " " + d.slice(3).join(":");
}
console.log(formatDate(new Date(new Date - 1))); // только что
console.log(formatDate(new Date(new Date - 30 * 1000))); // 30 сек. назад
console.log(formatDate(new Date(new Date - 5 * 60 * 1000))); // 5 мин. назад
console.log(formatDate(new Date(new Date - 86400 * 1000)));
function anotherFormatDate(date) {
var dd = date.getDate();
if (dd < 10) dd = "0" + dd;
var mm = date.getMonth() + 1;
if (mm < 10) mm = "0" + mm;
var yy = date.getFullYear() % 100;
if (yy < 10) yy = "0" + yy;
return dd + "." + mm + "." + yy;
}
var d = new Date(2014, 0, 30);
console.log(anotherFormatDate(d));