JavaScript how to get tomorrow's date in dd-mm-yy format

I am trying to get JavaScript to display tomorrow's date in the format (dd-mm-yyyy)

I have this script that displays today's date in the format (dd-mm-yyyy)

var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear() document.write("<b>" + day + "/" + month + "/" + year + "</b>") Displays: 25/2/2012 (todays date of this post) 

But how can I make it display tomorrow's date in the same format, i.e. 26/2/2012

I tried this:

 var day = currentDate.getDate() + 1 

However, I could keep +1 and go through 31, obviously there weren’t> 32 days a month

Was there a search for a watch, but there seemed to be no answer or solution?

+57
javascript
Feb 25 2018-12-12T00:
source share
10 answers

This should help you deal with this.

If you pass in the Date constructor, it will do the rest of the work.

24 hours 60 minutes 60 seconds 1000 milliseconds

 var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000); var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear() document.write("<b>" + day + "/" + month + "/" + year + "</b>") 

It should be borne in mind that this method will return the date exactly after 24 hours, which may be inaccurate in the summer.

Phil will reply at any time:

 var currentDate = new Date(); currentDate.setDate(currentDate.getDate() + 1); 

The reason I edited my post was because I myself created an error that was discovered during DST using my old method.

+121
Feb 25 2018-12-12T00:
source share

The JavaScript Date class handles this for you.

 var d = new Date("2012-02-29") console.log(d) // Wed Feb 29 2012 11:00:00 GMT+1100 (EST) d.setDate(d.getDate() + 1) console.log(d) // Thu Mar 01 2012 11:00:00 GMT+1100 (EST) console.log(d.getDate()) // 1 
+96
Feb 25 2018-12-12T00:
source share

I would use the DateJS library. He can do just that.

http://www.datejs.com/

Do the following:

 var d = new Date.today().addDays(1).toString("dd-mm-yyyy"); 

Date.today() - gives you today at midnight.

+5
Feb 25 2018-12-12T00:
source share

Use cases:

 Date.tomorrow() // 1 day next Date.daysNext(1) // alternative Date.tomorrow() Date.daysNext(2) // 2 days next. 

IF "tomorrow" does not depend on today, but on a different date other than Date.now() . Do not use static methods, but you should use non-static:

ie: Fri Dec 05 2008

  var dec5_2008=new Date(Date.parse('2008/12/05')); dec5_2008.tomorrow(); // 2008/12/06 dec5_2008.tomorrow().day // 6 dec5_2008.tomorrow().month // 12 dec5_2008.tomorrow().year //2008 dec5_2008.daysNext(1); // the same as previous dec5_2008.daysNext(7) // next week :) 

API:

 Dateold=Date;function Date(e){var t=null;if(e){t=new Dateold(e)}else{t=new Dateold}t.day=t.getDate();t.month=t.getMonth()+1;t.year=t.getFullYear();return t}Date.prototype.daysNext=function(e){if(!e){e=0}return new Date(this.getTime()+24*60*60*1e3*e)};Date.prototype.daysAgo=function(e){if(!e){e=0}return Date.daysNext(-1*e)};Date.prototype.tomorrow=function(){return this.daysNext(1)};Date.prototype.yesterday=function(){return this.daysAgo(1)};Date.tomorrow=function(){return Date.daysNext(1)};Date.yesterday=function(){return Date.daysAgo(1)};Date.daysNext=function(e){if(!e){e=0}return new Date((new Date).getTime()+24*60*60*1e3*e)};Date.daysAgo=function(e){if(!e){e=0}return Date.daysNext(-1*e)} 
+2
Nov 17 '14 at 10:24
source share

The Date.prototype.setDate () method accepts even arguments outside the standard range and changes the date accordingly.

 function getTomorrow() { const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); // even 32 is acceptable return `${tomorrow.getFullYear()}/${tomorrow.getMonth() + 1}/${tomorrow.getDate()}`; } 
+2
Apr 04 '15 at 1:25
source share

Method 1. If you have no problems using another library, this may work for you using moment.js

 moment().add('days', 1).format('L'); 

Method 2: Using Date.js,

 <script type="text/javascript" src="date.js"></script> var tomorrow = new Date.today().addDays(1).toString("dd-mm-yyyy"); 

This method uses an external library, not its own Date library. Since my bootstrap-datetimepicker used time.js and its own date library, I preferred method 1. This question mentions these and some other methods.

+1
Jun 23 '16 at 12:18
source share
 function getMonday(d) { // var day = d.getDay(); var day = @Config.WeekStartOn diff = d.getDate() - day + (day == 0 ? -6 : 0); return new Date(d.setDate(diff)); } 
0
Mar 18 '14 at 11:48
source share

It is very simple:

1: Create a date object with today's date and time. 2: Use date object methods to get the day, month, and full year and combine them using the + operator.

Visit http://www.thesstech.com/javascript/date-time JavaScript for a detailed date and time guide.

Code example:

  var my_date = new Date(); var tomorrow_date = (my_date .getDate()+1) + "-" + (my_date .getMonth()+1) + "-" + my_date .getFullYear(); document.write(tomorrow_date); 
0
Jun 23 '15 at 20:38
source share

Below is a combination of Roderick and Phil's answers with two additional conventions that take into account unambiguous months / days.

Many of the APIs I've worked with are picky about this, and require dates to have eight digits (for example, β€œ02022017”) instead of the 6 or 7 digits that will be assigned to the date class you are in some situations.

 function nextDayDate() { // get today date then add one var nextDay = new Date(); nextDay.setDate(nextDay.getDate() + 1); var month = nextDay.getMonth() + 1; var day = nextDay.getDate(); var year = nextDay.getFullYear(); if (month < 10) { month = "0" + month } if (day < 10) { day = "0" + day } return month + day + year; } 
0
Feb 28 '17 at 21:47
source share
  //-----------Date Configuration march 18,2014---------------------- //alert(DateFilter); var date = new Date(); y = date.getFullYear(), m = date.getMonth(); var EndDate = new Date(); switch (DateFilter) { case 'today': var StartDate = EndDate; //todays date break; case 'yesterday': var d = new Date(); var previousDate = new Date(d.getTime() - 1000 * 60 * 60 * 24); var StartDate = new Date(previousDate.yyyymmdd()); //yesterday Date break; case 'tomorrow': var d = new Date(); var NextDate = new Date(d.getTime() + 1000 * 60 * 60 * 24); var StartDate = new Date(NextDate.yyyymmdd()); //tomorrow Date break; case 'thisweek': var StartDate = getMonday(new Date()); //1st date of this week break; case 'thismonth': var StartDate = new Date(y, m, 1); //1st date of this month break; case 'thisyear': var StartDate = new Date("01/01/" + date.getFullYear()); //1st date of this year break; case 'custom': //var StartDate = $("#txtFromDate").val(); break; default: var d = new Date(); var StartDate = new Date(d.getTime() - 30 * 24 * 60 * 60 * 1000); //one month ago date from now. } if (DateFilter != "custom") { var SDate = $.datepicker.formatDate('@Config.DateFormat', StartDate); $("#txtFromDate").val(SDate); var EDate = $.datepicker.formatDate('@Config.DateFormat', EndDate); $("#txtToDate").val(EDate); } //-----------Date Configuration march 18,2014---------------------- 
-one
Mar 18 '14 at 11:40
source share



All Articles