Javascript 如何获得当前日期前 30 天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8842732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:45:59  来源:igfitidea点击:

How to get 30 days prior to current date?

javascriptjquery

提问by Jesse Atkinson

I have a start calendar input box and an 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().add(-30);工作,但我需要格式为January 13, 2012. 不是Fri Jan 13 2012 10:48:56 GMT -055 (EST)。有什么帮助吗?

MORE EDIT: As of this writing it's 2018. Just use Moment.js. It's the best.

更多编辑:在撰写本文时是 2018 年。只需使用Moment.js。这是最好的。

回答by Rory McCrossan

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 时间戳格式返回。要将其转换回日期对象,您需要将上述内容传递给新的日期对象;new Date(priorDate).

回答by maerics

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

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

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

[Edit]

[编辑]

See also the excellent Moment.js JavaScript date library:

另请参阅优秀的Moment.js JavaScript 日期库

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

回答by Nahn

Here's an ugly solution for you:

这是一个丑陋的解决方案:

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

回答by Marc B

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 时间戳(自 1970 年 1 月 1 日以来的毫秒数),您可以在该时间戳上使用常规数学运算,这些运算可以直接反馈给 Date 对象。

回答by WasiF

Get next 30 days from today

从今天开始接下来的 30 天

let now = new Date()
console.log('Today: ' + now.toUTCString())
let next30days = new Date(now.setDate(now.getDate() + 30))
console.log('Next 30th day: ' + next30days.toUTCString())

Get last 30 days form today

今天获取过去 30 天的表格

let now = new Date()
console.log('Today: ' + now.toUTCString())
let last30days = new Date(now.setDate(now.getDate() - 30))
console.log('Last 30th day: ' + last30days.toUTCString())

回答by Freddy

Javascript can handle it without any external libraries.

Javascript 可以在没有任何外部库的情况下处理它。

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

document.write(today + "<br/>" + dateLimit)

回答by manish kumar

Easy.(Using Vanilla JS)

简单。(Using Vanilla JS)

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

ISOFormat ?

ISO格式?

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

回答by LMK

I will prefer moment js

我会更喜欢moment js

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

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

回答by Diodeus - James MacFarlane

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

我使用date.js。它可以轻松处理这个问题并处理所有闰年的问题。

回答by Lawrence Edmondson

This is an ES6 version

这是一个 ES6 版本

let date = new Date()
let newDate = new Date(date.setDate(date.getDate()-30))
console.log(newDate.getMonth()+1 + '/' + newDate.getDate() + '/' + newDate.getFullYear() )