English 中文(简体)
如何在目前日期之前30天?
原标题:How to get 30 days prior to current date?

我有一个起始日历输入箱和一个最后日历输入箱。 我们希望缺省在本日期之前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();

截止日期为<代码>var enddate = 起始日期-30;。 显然,这赢得了工作。

因此,如果目前的日期是2011年12月30日,我就希望开始日期改为2011年12月1日。

EDIT:我的问题得到了回答:Date.today(); and Date.today(-30); work,但我需要以January 13, 2012格式的日期。 Not Fri Jan 13 2012 10:48:56 GMT -055 (EST) 。 任何帮助?

MORE EDIT:截至本文编写时,它为2018年。 Just use Moment.js。 它是最好的。

最佳回答
问题回答

从一份联合材料中减去数日,您可以使用<代码>setDate()方法,同时从计算开始的日期开始计算。 这将把一个百科全书重新作为星号,以便将其改为需要再次向<代码>Date(客座构造者提供的日期。 最后的例子就是这样:

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

console.log(today)
console.log(priorDate);

这里,你的解决办法是:

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

<代码>.gettime()方法回收了标准的联合调查时间(自1970年1月1日起为百万分之十二),可以据此定期使用数学操作,可以直接反馈到日标。

www.un.org/Depts/DGACM/index_spanish.htm 第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())

www.un.org/Depts/DGACM/index_spanish.htm 第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())

贾瓦特可以在没有外部图书馆的情况下处理。

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

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

Simple 1 liner Vanilla Javascript code :

const priorByDays = new Date(Date.now() - days * 24 * 60 * 60 * 1000)

For example: days = 7 Assume current date = Fri Sep 18 2020 01:33:26 GMT+0530

www.un.org/Depts/DGACM/index_spanish.htm 结果是:Fri Sep 11 2020 01:34:03 GMT+0530

这是你可以操纵的,以便产生理想的类型

  • 页: 1

  • ISOString:new Annual(Date.now() - 7 * 24 * 60 * 60 * 10)。

简明代码(Using Vanilla JS)

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

ISOFormat?

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

www.un.org/Depts/DGACM/index_french.htm

let final30Days = 新的日期(today.getFull Year(),今天,getMonth(),今天,植被Date() - 30)

我更喜欢<>升空js。

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

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

http://www.datejs.com/“rel=“nofollow”>date.js。 它很容易地处理这种情况,并照顾到所有一年的新生。

这是ES-6版本。

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

仅通过使用Node JS.的1行法典,你就可以这样做:

    let lastOneMonthDate = moment().subtract(30,"days").utc().toISOString()

Don tech UTC Format, EASIER :P

    let lastOneMonthDate = moment().subtract(30,"days").toISOString()

如果你有点 t,你可以这样做:

<代码>X = 新的日期(

x.toISOString(x.setDate(x.getDate())).slice(0, 10)

基本来说,它达到了目前的日期(现月的计算值),然后确定价值。 然后改为标准化组织格式,从中删除纯数字日期(即2019-09-23年)。

Hope it helps someone.

任何人,如果想到这种格式,就为我工作:

let date = new Date()

let newDate = new Date(date.setDate(date.getDate()-30))

console.log(newDate.getDate()+     +newDate.toLocaleString( default , {month:  long }) +     + newDate.getFullYear() )

Use moment.js

let startDate =  moment().subtract(30, "days").format( YYYY-MM-DD ); //2021-05-18
let endDate = moment().format( YYYY-MM-DD ); //2021-06-17




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签