How to get 30 days before the current date?

I have an input field for the start calendar and an input field for the end calendar. We want the default calendar entry field to start 30 days before the current date, and the end calendar entry field should be the current date. Here is my date

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 will look something like this: var enddate = startdate - 30; Obviously this will not work.

Therefore, if the current date is December 30, 2011, I would like the start date to be read on December 1, 2011.

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

EDIT MORE: At the time of writing, this is 2018. Just use Moment.js . This is the best.

+80
javascript jquery
Jan 12 2018-12-12T00:
source share
12 answers

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

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

[change]

See also the excellent Moment.js JavaScript date :

 moment().subtract(30, 'days'); // or... moment().add(-30, 'days'); 
+125
Jan 12 2018-12-12T00:
source share

try it

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

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

+128
Jan 12 2018-12-12T00:
source share

Here's an ugly solution for you:

 var date = new Date(new Date().setDate(new Date().getDate() - 30)); 
+74
Jul 27 '15 at 23:47
source share
 startDate = new Date(today.getTime() - 30*24*60*60*1000); 

The .getTime() method returns the standard JS timestamp (milliseconds since January 1/1970), on which you can use regular mathematical operations that can be returned directly to the Date object.

+34
Jan 12 2018-12-12T00:
source share

Javascript can handle this without any external libraries.

 var today = new Date(); var dateLimit = new Date(new Date().setDate(today.getDate() - 30)); document.write(today + "<br/>" + dateLimit) 
+2
Aug 01 '18 at 10:16
source share

Get the next 30 days from today

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

Get the last 30 days of the form today

 let now = new Date() console.log('Today: ' + now.toUTCString()) let last30days = new Date(now.setDate(now.getDate() - 30)) console.log('Previous: ' + last30days.toUTCString()) 
+2
Mar 22 '19 at 8:16
source share

I am using date.js. He easily copes with this and takes care of all the leap bastards.

+1
Jan 12 '12 at 21:40
source share

I prefer the moment js

 startDate = moment().subtract(30, 'days').format('LL') // January 29, 2015 endDate = moment().format('LL'); // February 28, 2015 
+1
Feb 28 '15 at 15:37
source share

Easily.

 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(); 
+1
Jun 21 '18 at 17:11
source share

This is ES6 version.

 let date = new Date() let newDate = new Date(date.setDate(date.getDate()-30)) console.log(newDate.getMonth()+1 + '/' + newDate.getDate() + '/' + newDate.getFullYear() ) 
0
Aug 13 '18 at 21:20
source share

You can do this simply in 1 line of code using the moment in Node JS. :)

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

I do not want UTC format, EASY: P

  let lastOneMonthDate = moment().subtract(30,"days").toISOString() 
0
Jan 09 '19 at 10:25
source share

If you are not inclined to take advantage of the points, you can use this:

let x = new Date()

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

It basically gets the current date (the numerical value of the date of the current month), and then sets the value. Then it is converted to ISO format, from which I shorten the net numeric date (i.e. 2019-09-23).

Hope this helps someone.

0
Sep 23 '19 at 9:16
source share



All Articles