Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript how to get tomorrows date in format dd-mm-yy

Tags:

javascript

I am trying to get JavaScript to display tomorrows date in format (dd-mm-yyyy)

I have got this script which displays todays date in 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 do I get it to display tomorrows 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 over 31 obviously there are not >32 days in a month

Been searching for hours but seems to be no answer or solution around this?

like image 646
daza166 Avatar asked Feb 25 '12 14:02

daza166


People also ask

How can I get tomorrow date in moment?

We can get tomorrow's date with the moment function and the add method. And then we can format it into a human-readable date string with the format method. to create the tomorrow variable that's set to a moment object with today's date. Then we call add with 1 and 'days' to add one day to today.

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy: Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

How do you code a date in JavaScript?

We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax. Syntax: new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hours, minutes, seconds, milliseconds);

What is new date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.


9 Answers

This should fix it up real nice for you.

If you pass the Date constructor a time 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>")

One thing to keep in mind is that this method will return the date exactly 24 hours from now, which can be inaccurate around daylight savings time.

Phil's answer work's anytime:

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

The reason I edited my post is because I myself created a bug which came to light during DST using my old method.

like image 124
Roderick Obrist Avatar answered Sep 24 '22 16:09

Roderick Obrist


The JavaScript Date class handles this for you

var d = new Date(2012, 1, 29) // month is 0-based in the Date constructor
console.log(d.toLocaleDateString())
// Wed Feb 29 2012

d.setDate(d.getDate() + 1)
console.log(d.toLocaleDateString())
// Thu Mar 01 2012

console.log(d.getDate())
// 1
like image 24
Phil Avatar answered Sep 24 '22 16:09

Phil


Method Date.prototype.setDate() 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()}`;
}
like image 21
czerny Avatar answered Sep 25 '22 16:09

czerny


Using JS only(Pure js)

Today

new Date()
//Tue Oct 06 2020 12:34:29 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0, 0))
//Tue Oct 06 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(new Date().setHours(0, 0, 0,0)).toLocaleDateString('fr-CA')
//"2020-10-06"

Tomorrow

new Date(+new Date() + 86400000);
//Wed Oct 07 2020 12:44:02 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0, 0) + 86400000);
//Wed Oct 07 2020 00:00:00 GMT+0530 (India Standard Time)
new Date(+new Date().setHours(0, 0, 0,0)+ 86400000).toLocaleDateString('fr-CA')
//"2020-10-07"
//don't forget the '+' before new Date()

Day after tomorrow

Just multiply by two ex:- 2*86400000

You can find all the locale shortcodes from https://stackoverflow.com/a/3191729/7877099

like image 42
Isuru Dilshan Avatar answered Sep 25 '22 16:09

Isuru Dilshan


I would use the DateJS library. It can do exactly that.

http://www.datejs.com/

The do the following:

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

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

like image 39
MattW Avatar answered Sep 22 '22 16:09

MattW


The below uses a combination of Roderick and Phil's answers with two extra conditionals that account for single digit months/days.

Many APIs I've worked with are picky about this, and require dates to have eight digits (eg '02022017'), instead of the 6 or 7 digits the date class is going to give you in some situations.

function nextDayDate() {
      // get today's 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;
}
like image 38
DNestoff Avatar answered Sep 24 '22 16:09

DNestoff


Use cases :

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

IF "tomorrow " is not depend of today but of another Date different of Date.now(), Don't use static methods but rather you must use non-static :

i.e: 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)}
like image 38
Abdennour TOUMI Avatar answered Sep 23 '22 16:09

Abdennour TOUMI


Method 1: If you don't have problem in using other library, then this could 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 external library and not the native Date library. As my bootstrap-datetimepicker was using moment.js and native date library, I preferred method 1. This question mentions these and some other methods.

like image 37
Shiwangi Avatar answered Sep 22 '22 16:09

Shiwangi


Its really simple:

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

Sample Code:

var my_date = new Date();
var tomorrow_date = (my_date.getDate() + 1) + "-" + (my_date.getMonth() + 1) + "-" + my_date.getFullYear();
document.write(tomorrow_date);
like image 27
Sohail Arif Avatar answered Sep 24 '22 16:09

Sohail Arif