如何在当前日期前30天到达?

时间:2022-08-25 17:52:36

I have a start calendar input box and a end calendar input box. We want defaults start calendar input box 30 days prior to current date and the end calendar input box to be the current date. Here is my date vars.

我有一个开始日历输入框和一个结束日历输入框。我们希望默认的开始日历输入框在当前日期前30天,结束日历输入框为当前日期。这是我的约会资料。

var today = new Date(),
    dd    = today.getDate(),
    mm    = today.getMonth(),
    yyyy  = today.getFullYear(),
    month = ["January", "February", "March",
        "April", "May", "June", "July", "August",
        "September", "October" "November", "December"],
    startdate = month[mm] + ", " + yyyy.toString();

The end date would be something like var enddate = startdate - 30; Obviously this won't work.

结束日期应该是var enddate = startdate - 30;这显然是行不通的。

So if the current date is December 30, 2011 I'd want the start date to read December 1, 2011.

如果当前日期是2011年12月30日,我希望开始日期是2011年12月1日。

EDIT: My question was answered... sort of. Date.today(); and Date.today().add(-30); work but I need the date in the format of January 13, 2012. Not Fri Jan 13 2012 10:48:56 GMT -055 (EST). Any help?

我的问题被回答了……排序的。Date.today();阀门和Date.today()(-30);工作,但我需要日期格式为2012年1月13日。不是2012年1月13日星期五10:48:56 GMT -055。任何帮助吗?

7 个解决方案

#1


95  

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

尝试使用优秀的Datejs JavaScript日期库(原始版本不再被维护,因此您可能会对这个积极维护的fork感兴趣):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

[Edit]

(编辑)

See also the excellent Moment.js JavaScript date library:

也要看精彩的时刻。js JavaScript库日期:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

#2


107  

Try this

试试这个

var today = new Date()
var priorDate = new Date().setDate(today.getDate()-30)

As noted by @Neel, this method returns in Javascript Timestamp format. To convert it back to date object, you need to pass the above to a new Date object; new Date(priorDate).

正如@Neel所指出的,此方法返回Javascript时间戳格式。要将其转换回date对象,您需要将上面的内容传递给新的日期对象;新的日期(priorDate)。

#3


56  

Here's an ugly solution for you:

这里有一个丑陋的解决方案:

var date = new Date(new Date().setDate(new Date().getDate() - 30));

#4


34  

startDate = new Date(today.getTime() - 30*24*60*60*1000);

The .getTime() method returns a standard JS timestamp (milliseconds since Jan 1/1970) on which you can use regular math operations, which can be fed back to the Date object directly.

gettime()方法返回一个标准的JS时间戳(自1/1970年1月以来的毫秒数),您可以在该时间戳上使用常规的数学操作,这些操作可以直接反馈给Date对象。

#5


1  

I use date.js. It handles this easily and takes care of all the leap-year nastiness.

我使用date.js。它很容易处理这个问题,并处理所有跨越式的不愉快。

#6


1  

I will prefer moment js

我更喜欢瞬间js

startDate = moment().subtract(30, 'days').format('LL')  // January 29, 2015

endDate = moment().format('LL'); // February 28, 2015

#7


0  

Easy.

一件容易的事。

let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000);

ISOFormat ?

ISOFormat吗?

let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();

#1


95  

Try using the excellent Datejs JavaScript date library (the original is no longer maintained so you may be interested in this actively maintained fork instead):

尝试使用优秀的Datejs JavaScript日期库(原始版本不再被维护,因此您可能会对这个积极维护的fork感兴趣):

Date.today().add(-30).days(); // or...
Date.today().add({days:-30});

[Edit]

(编辑)

See also the excellent Moment.js JavaScript date library:

也要看精彩的时刻。js JavaScript库日期:

moment().subtract(30, 'days'); // or...
moment().add(-30, 'days');

#2


107  

Try this

试试这个

var today = new Date()
var priorDate = new Date().setDate(today.getDate()-30)

As noted by @Neel, this method returns in Javascript Timestamp format. To convert it back to date object, you need to pass the above to a new Date object; new Date(priorDate).

正如@Neel所指出的,此方法返回Javascript时间戳格式。要将其转换回date对象,您需要将上面的内容传递给新的日期对象;新的日期(priorDate)。

#3


56  

Here's an ugly solution for you:

这里有一个丑陋的解决方案:

var date = new Date(new Date().setDate(new Date().getDate() - 30));

#4


34  

startDate = new Date(today.getTime() - 30*24*60*60*1000);

The .getTime() method returns a standard JS timestamp (milliseconds since Jan 1/1970) on which you can use regular math operations, which can be fed back to the Date object directly.

gettime()方法返回一个标准的JS时间戳(自1/1970年1月以来的毫秒数),您可以在该时间戳上使用常规的数学操作,这些操作可以直接反馈给Date对象。

#5


1  

I use date.js. It handles this easily and takes care of all the leap-year nastiness.

我使用date.js。它很容易处理这个问题,并处理所有跨越式的不愉快。

#6


1  

I will prefer moment js

我更喜欢瞬间js

startDate = moment().subtract(30, 'days').format('LL')  // January 29, 2015

endDate = moment().format('LL'); // February 28, 2015

#7


0  

Easy.

一件容易的事。

let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000);

ISOFormat ?

ISOFormat吗?

let days=30;
this.maxDateTime = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();