<!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>
运行结果:
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vuex的state状态对象使用方式汇总
react+redux从零开始
以上就是如何使用js取得最近7天与最近3天日期的详细内容。
