当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JS实现简单获取最近7天和最近3天日期的方法

JS实现简单获取最近7天和最近3天日期的方法

2018年04月28日  | 移动技术网IT编程  | 我要评论

本文实例讲述了js实现简单获取最近7天和最近3天日期的方法。分享给大家供大家参考,具体如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>www.jb51.net 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>

运行结果:

ps:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用:

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

在线天数计算器:

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript时间与日期操作技巧总结》、《javascript+html5特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网