Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

javascript - 从JavaScript中的日期中减去天(Subtract days from a date in JavaScript)

Does anybody know of an easy way of taking a date (eg Today) and going back X days?(有谁知道约会(例如今天)和回溯X天的简单方法吗?)

So, for example, if I want to calculate the date 5 days before today.(因此,例如,如果我想计算今天前5天的日期。)   ask by jonhobbs translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:(尝试这样的事情:)

var d = new Date(); d.setDate(d.getDate()-5); Note that this modifies the date object and returns the time value of the updated date.(请注意,这将修改日期对象并返回更新日期的时间值。) var d = new Date(); document.write('Today is: ' + d.toLocaleString()); d.setDate(d.getDate() - 5); document.write('<br>5 days ago was: ' + d.toLocaleString());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...