<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
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 d1 = new Date().Format("yyyy-MM-dd");
alert(d1);
var d2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
alert(d2);
</script>
</head>
<body>
</body>
</html>
时间日期比较:
var subscribetime = $("#apply_time").val();
var curTime = new Date().Format("yyyy-MM-dd");
if(subscribetime <= curTime){
swal('提示', '请提前一天预约!', 'warning');
return false;
}
本文介绍了一种使用JavaScript进行日期格式化的实用方法,并演示了如何比较两个日期,确保预约时间符合业务逻辑。通过自定义Date原型的方法,可以轻松地将日期转换为指定格式,如yyyy-MM-dd或yyyy-MM-dd HH:mm:ss。
344





