var time = new Date();
var nowDay = time.toLocaleDateString().replace(/\//g, '-');
var createtime_Y = unix_to_datetime(1459407384, 1);
function unix_to_datetime(unix, type) {
var now = new Date(parseInt(unix) * 1000);
var Y = now.getFullYear() + '-';
//0-11,0代表1月
var M = now.getMonth()+1 + '-';
var D = now.getDate() + ' ';
var h = (now.getHours() < 10 ? '0'+(now.getHours()) : now.getHours()) + ':';
var m = (now.getMinutes() < 10 ? '0'+(now.getMinutes()) : now.getMinutes()) + ':';
var s = (now.getMinutes() < 10 ? '0'+(now.getMinutes()) : now.getMinutes());
switch(type) {
case 1:
return Y+M+D;
case 2:
return h+m+s;
case 3:
var M = (now.getMonth()+1 < 10 ? '0'+(now.getMonth()+1) : now.getMonth()+1) + '月';
return M+D+h+m+s;
}
}
console.log(nowDay);
console.log(typeof(nowDay));
console.log(createtime_Y);
console.log(typeof(createtime_Y));
if (nowDay == createtime_Y) {
console.log(1);
} else {
console.log(2);
}
如图?why?
为什么输出的是2???求解
nowDay 31后面有空格。去掉空格试试吧,不对的话我就不知道了。另外==等号不是严格相等,不用查看类型,类型不一样也相等。