目的:记录中展现“今天”,“明天”,除外展现月日。

借鉴:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>js获取日期:前天、昨天、今天、明天、后天 - www.vnote.net</title>
</head> <body>
<script language="JavaScript" type="text/javascript">
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
document.write("前天:"+GetDateStr(-2));
document.write("<br />昨天:"+GetDateStr(-1));
document.write("<br />今天:"+GetDateStr(0));
document.write("<br />明天:"+GetDateStr(1));
document.write("<br />后天:"+GetDateStr(2));
document.write("<br />大后天:"+GetDateStr(3));
</script>
</body>
</html>

改后我需要的:

逻辑:我需要获取今天的时间day1,还需要会议的时间day2;将两个时间相减去,day1-day2值为0,显示今天;值为-1时,显示明天;值为这两种值之外,显示月,日。

进一步参考:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> </body>
</html>
<script type="text/javascript"> function dateDiff(date1, date2) {
var type1 = typeof date1, type2 = typeof date2;
if (type1 == 'string')
date1 = stringToTime(date1);
else if (date1.getTime)
date1 = date1.getTime();
if (type2 == 'string')
date2 = stringToTime(date2);
else if (date2.getTime)
date2 = date2.getTime();
//alert((date1 - date2) / (1000*60*60));
return (date1 - date2) / (1000 * 60 * 60 * 24); //结果是小时
}
//字符串转成Time(dateDiff)所需方法
function stringToTime(string) {
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);
return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1) - 1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime();
}
t = dateDiff("2016-11-24", "2016-11-24");
if(t==0){
alert("今天");
}else if(t==-1){
alert("明天");
}else{
alert("某天");
}
</script>
05-11 17:04