这次给大家带来如何使用JS取得最近7天与最近3天日期,使用JS取得最近7天与最近3天日期的注意事项有哪些,下面就是实战案例,一起来看一下。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> JS获取最近三天和最近3天日期</title>
</head>
<body>
<script>
//获取最近7天日期
console.log(getDay(0));//当天日期
console.log(getDay(-7));//7天前日期
//获取最近3天日期
console.log(getDay(0));//当天日期
console.log(getDay(-3));//3天前日期
function getDay(day){
var today = new Date();
var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
today.setTime(targetday_milliseconds); //注意,这行是关键代码
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear+"-"+tMonth+"-"+tDate;
}
function doHandleMonth(month){
var m = month;
if(month.toString().length == 1){
m = "0" + month;
}
return m;
}
</script>
</body>
</html>运行结果:

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是如何使用JS取得最近7天与最近3天日期的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号