Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Js

Idiom #184 Tomorrow

Assign to t a string representing the day, month and year of the day after the current date.

var now = new Date()
var year = now.getFullYear()
var month = now.getMonth()
var day = now.getDate()

var tomorrow = new Date(0)
tomorrow.setFullYear(year, month, day + 1)
tomorrow.setHours(0, 0, 0, 0)

var shortDateFormat = Intl.DateTimeFormat(undefined, { dateStyle: "short" })
var t = shortDateFormat.format(tomorrow)

Adding 24 hours to the current time does not always get tomorrow because there could be a daylight saving time transition.

The user may not use "dd/mm/yyyy" as their preferred date format. shortDateFormat is a formatter that honors the user's preferences.
let t = new Date();
t.setDate(t.getDate() + 1)
t = t.toString()

"... If you specify a number outside the expected range, the date information in the Date object is updated accordingly."
var nextDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = nextDate.getDate()
var month = nextDate.getMonth() + 1
var year = nextDate.getFullYear()
var t = `${day}/${month}/${year}`;

Note that Date.prototype.getMonth() is zero-based.
t is not zero padded, so it may have a single-digit day or month.
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var t = DateTime.Today.AddDays(1).ToShortDateString();

You can start with DateTime.Today or with DateTime.Now

Today will include the current date, whereas Now will also include the time of day.

if you add 1 day to now, you will get a time of day tomorrow.
e.g. 9:15 AM today plus 1 day = 9:15 AM tomorrow

New implementation...
< >
steenslag