今天三个月前如何计算JavaScript的日期? [英] How do I calculate the date in JavaScript three months prior to today?
本文介绍了今天三个月前如何计算JavaScript的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在当前日期前3个月形成日期。我通过以下代码获得当前月份$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
var currentMonth = currentDate.getMonth()+ 1;你可以为我提供计算和形成一个日期的逻辑(一个<$ c的对象)
考虑到当月份为1月(1)时,日期为$ c> Date 数据类型),日期前3个月将为OCtober(10)?
解决方案
var d = new Date();
d.setMonth(d.getMonth() - 3);
这适用于1月份。运行此代码段:
var d = new Date(2012-01-15); document.write(d +< br />); d.setMonth(d.getMonth() - 3); document.write(d);
p>
I Am trying to form a date which is 3 months before the current date. I get the current month by the below code
var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;
Can you guys provide me the logic to calculate and form a date (an object of the Date
data type) considering that when the month is January (1), 3 months before date would be OCtober (10)?
解决方案
var d = new Date();
d.setMonth(d.getMonth() - 3);
This works for January. Run this snippet:
var d = new Date("2012-01-15");
document.write(d + "<br/>");
d.setMonth(d.getMonth() - 3);
document.write(d);
这篇关于今天三个月前如何计算JavaScript的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文