Getting next day date in javascript

Here is an useful function to get next day’s date string in javascript. I am going to explain each line of code for those who like to ponder.

function getDateOfNextDay(datestring, separator, nozero)	{

  if(!separator)	{
    separator="-";//="yyyy-dd-mm" format 
  }

  var a_date = datestring.split(separator);
  var myday = new Date(a_date[0]+'/'+a_date[1]+'/'+a_date[2]);
  myday.setDate(myday.getDate()+1);

  var next_day_year = myday.getFullYear();
  var next_day_month = myday.getMonth()+1;

  if(!nozero)	{
    next_day_month = (parseInt(next_day_month)<10)?"0"+next_day_month:next_day_month;
  }

  var next_day_day = myday.getDate();
  next_day_day = (parseInt(next_day_day)<10)?"0"+next_day_day:next_day_day;

  return next_day_month+"-"+next_day_day+"-"+next_day_year;
}

Ok. We start with the function call itself.

getDateOfNextDay(datestring, separator, nozero);

The function call takes a “datestring” as first argument in yyyy-dd-mm format. The second argument is the “separator” used to glue yyyy, dd and mm values togther. We will discuss “nozero” down later.

  if(!separator)	{
    separator="-";//="yyyy-dd-mm" format 
  }

If no separator is passed as the second argument “-” is considered as the default separator.

var a_date = datestring.split(separator);

We split “datestring” on the “separator” to form a new array “a_date” of values. First, second and third part contains year, day and month respectively. We are splitting the datestring here so that we can use them while creating new date object using Javascript’s inbuilt “Date” class object. Usually we pass a date string to the date class in yyyy/dd/mm format so if your “datestring” is already in this format you can consider skipping the above two steps. But this method (above two steps) is recommended as it takes care of dates in different formats.

var myday = new Date(a_date[0]+'/'+a_date[1]+'/'+a_date[2]); 

Creating date object by passing date in yyyy/dd/mm format.

myday.setDate(myday.getDate()+1);

Requesting the “myday” object to set it to next day. Ok. The javascript date object already contains next day now. Now we will need to format our return date string.

var next_day_year = myday.getFullYear();
var next_day_month = myday.getMonth()+1;

In two lines above “next_day_year” picks year offset of the next day while “next_day_month” gets the month offset of the next day.

if(!nozero)	{
    next_day_month = (parseInt(next_day_month)<10)?"0"+next_day_month:next_day_month;
}

This is a customization of the return value which is optional. I wanted to prefix my month offset with 0(zero) to make it look like 09 rather than 9. I wanted to look my “day” offset always like this so i did not impose a conditions to that below. If this (day or month offset) is not a integer value greater than 10 we would prefixes a “0” to the day or month offset accordingly. (below as well)

var next_day_day = myday.getDate();
next_day_day = (parseInt(next_day_day)<10)?"0"+next_day_day:next_day_day;

And finally,

return next_day_month+"-"+next_day_day+"-"+next_day_year;

All is set now. Why don’t return a well formatted date of next day.

I hope you enjoyed this post! Don’t forgot to put your thoughts. They (your comments) always motivate me to make postings.

Leave a Reply