<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div{
padding: 10px 0;
}
</style>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div>
<button class="preDay">点击获取昨天日期</button>:<span class="preDate"> </span>
</div>
<div>
<button>我就是今天的日期</button>:<span class='tody'></span>
</div>
<div>
<button class="nextDay">点击获取明天日期</button>:<span class="nextDate"></span>
</div>
<script>
dateFormat = (date, fmt) => {
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.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;
};
function getNextDay(d){
d = new Date(d);
d = +d + 1000*60*60*24;
d = new Date(d);
//格式化
return dateFormat(d, "yyyy-MM-dd");
}
function getPrevDay(d){
d = new Date(d);
d = d - 1000*60*60*24;
d = new Date(d);
//格式化
return dateFormat(d, "yyyy-MM-dd");
}
$('.preDay').on('click', function(){
$('.preDate').html(getPrevDay(new Date()));
});
$('.nextDay').on('click', function(){
$('.nextDate').html(getNextDay(new Date()));
});
$('.tody').html(dateFormat(new Date(), "yyyy-MM-dd"));
</script>
</body>
</html>
转载于:https://www.cnblogs.com/fangfeiyue/p/7364580.html