DateUtil.js
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
调用方法
var now = new Date();
var today = now.format('yyyy-MM-dd');// yyyy-MM-dd 可自定义
var nowTime = now.format('hh:mm');// hh:mm 可自定义
console.log(today+' '+nowTime);
日期比较
var todayTime = parseInt(new Date().format('yyyyMMddhhmmss'));
var Timestart = parseInt(app.noticeObj.bday.replace(/-/g, '').replace(' ', '').replace(/:/g, ''));
var TimeEnd = parseInt(app.noticeObj.eday.replace(/-/g, '').replace(' ', '').replace(/:/g, ''));
if (todayTime < Timestart||todayTime>TimeEnd) {
// 当前时间小于开始时间 或 当前时间大于结束时间 (不弹出)
window.location.href="page1.html"
}else {
// 开始时间<当前时间<结束时间(弹出)
mui.alert(app.noticeObj.msg, '温馨提示', function () {
window.location.href="page2.html"
});
}