-
Notifications
You must be signed in to change notification settings - Fork 0
/
日期.html
105 lines (96 loc) · 3.38 KB
/
日期.html
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>js获取日期:前天、昨天、今天、明天、后天 </title>
</head>
<body>
<div style="cursor: not-allowed;pointer-events: none;">121121</div>
<script language="JavaScript" type="text/javascript">
var str = 1478508411000;
console.log(new Date(str).toDateString());
if(new Date(str).toDateString() === new Date().toDateString()) {
//今天
console.log("当天");
} else if (new Date(str) < new Date()) {
//之前
console.log(new Date(str).toISOString().slice(0, 10));
}
//函数形式
function isToday(str) {
console.log(new Date(str).toDateString() );
console.log( new Date().toDateString());
if(new Date(str).toDateString() === new Date().toDateString()) {
//今天
console.log("当天");
} else if(new Date(str) < new Date()) {
//之前
console.log("以前的日期");
}
}
var str = 1499386322;
isToday(str);
/*======================分割线========================*/
function judgeTimeStamp (stamp) {
var stamp = parseInt(parseFloat(stamp)*1000);
console.log(stamp);
returnStr = {
'today': '今天',
'yesterday': '昨天',
'beforeYesterday ': '前天',
'threeDaysAgo': '3天前',
'aWeekAgo': '一周前',
'aMothAgo ': '一个月前',
'aYearAgo':'一年前'
},
designatedTime = function (stamp) {
var today = new Date(stamp),
year = parseFloat(today.getFullYear()),
month= parseFloat(today.getMonth()+1),
day = parseFloat(today.getDate()),
hour = parseFloat(today.getHours());
return {
year: year,
month: month,
day: day,
hour: hour
}
},
CurrentTime = function () {
var today = new Date(),
year = parseFloat(today.getFullYear()),
month = parseFloat(today.getMonth()+1),
day = parseFloat(today.getDate()),
hour = parseFloat(today.getHours());
return {
year: year,
month: month,
day: day,
hour: hour
}
};
var designatedTimeObj = designatedTime(stamp),
currentTimeObj = CurrentTime();
if (currentTimeObj.year === designatedTimeObj.year) {//同一年
if (currentTimeObj.month === designatedTimeObj.month) {//同一月
if (currentTimeObj.day === designatedTimeObj.day) {//同一天.今天
return returnStr.today;
} else if (currentTimeObj.day - designatedTimeObj.day == 1) {//昨天
return returnStr.yesterday;
} else if (currentTimeObj.day - designatedTimeObj.day == 2 ) {//前天
return returnStr.beforeYesterday;
} else if (currentTimeObj.day - designatedTimeObj.day > 2) {//三天前
return returnStr.threeDaysAgo;
} else if (currentTimeObj.day - designatedTimeObj.day >= 6) {//一周前
return returnStr.aWeekAgo;
}
} else if (currentTimeObj.month - designatedTimeObj.month >= 1) {
return returnStr.aMothAgo ;
}
} else if (currentTimeObj.year - designatedTimeObj.year >= 1) {//一年前
return returnStr.aYearAgo;
}
}
console.log(judgeTimeStamp(1499386322.572344));
</script>
</body>
</html>