#JavaScript April 03, 2023

Notes on JS Date & Time

A list of some common JavaScript date and time functions:

Date(): This function returns the current date and time.
const currentDate = new Date();
console.log(currentDate);
// Output: Mon Apr 03 2023 06:05:39 GMT+0800 (Philippine Standard Time)

new Date(milliseconds): This function returns a Date object representing a specific date and time, based on a number of milliseconds since January 1, 1970, 00:00:00 UTC.
const specificDate = new Date(1617298800000);
console.log(specificDate);
// Output: Fri Apr 02 2021 01:40:00 GMT+0800 (Philippine Standard Time)

getTime(): This function returns the number of milliseconds between a specified date and time and midnight January 1, 1970.
const currentDate = new Date();
const milliseconds = currentDate.getTime();
console.log(milliseconds);
// Output: 1680473218923

getFullYear(): This function returns the year of a specified date and time.
const currentDate = new Date();
const year = currentDate.getFullYear();
console.log(year);
// Output: 2023

getMonth(): This function returns the month of a specified date and time, where 0 is January and 11 is December.
const currentDate = new Date();
const month = currentDate.getMonth();
console.log(month);
// Output: 3 (April, zero-based index)

getDate(): This function returns the day of the month of a specified date and time.
const currentDate = new Date();
const dayOfMonth = currentDate.getDate();
console.log(dayOfMonth);
// Output: 3

getDay(): This function returns the day of the week of a specified date and time, where 0 is Sunday and 6 is Saturday.
const currentDate = new Date();
const dayOfWeek = currentDate.getDay();
console.log(dayOfWeek);
// Output: 1 (Monday)

getHours(): This function returns the hour of a specified date and time.
const currentDate = new Date();
const hours = currentDate.getHours();
console.log(hours);
// Output: 6

getMinutes(): This function returns the minute of a specified date and time.
const currentDate = new Date();
const minutes = currentDate.getMinutes();
console.log(minutes);
// Output: 9

getSeconds(): This function returns the second of a specified date and time.
const currentDate = new Date();
const seconds = currentDate.getSeconds();
console.log(seconds);
// Output: 37

setFullYear(year): This function sets the year of a specified date and time.
const currentDate = new Date();
currentDate.setFullYear(2022);
console.log(currentDate);
// Output: Sun Apr 03 2022 06:09:49 GMT+0800 (Philippine Standard Time)

setMonth(month): This function sets the month of a specified date and time, where 0 is January and 11 is December.
const currentDate = new Date();
currentDate.setMonth(6); // Note: 6 is July
console.log(currentDate);
// Output: Mon Jul 03 2023 06:10:10 GMT+0800 (Philippine Standard Time)

setDate(day): This function sets the day of the month of a specified date and time.
const currentDate = new Date();
currentDate.setDate(15);
console.log(currentDate);
// Output: Sat Apr 15 2023 06:11:37 GMT+0800 (Philippine Standard Time)

setHours(hour): This function sets the hour of a specified date and time.
const currentDate = new Date();
currentDate.setHours(14);
console.log(currentDate);
// Output: Mon Apr 03 2023 14:12:01 GMT+0800 (Philippine Standard Time)

setMinutes(minute): This function sets the minute of a specified date and time.
const currentDate = new Date();
currentDate.setMinutes(30);
console.log(currentDate);
// Output: Mon Apr 03 2023 06:30:23 GMT+0800 (Philippine Standard Time)

setSeconds(second): This function sets the second of a specified date and time.
const currentDate = new Date();
currentDate.setSeconds(45);
console.log(currentDate);
// Output: Mon Apr 03 2023 06:12:45 GMT+0800 (Philippine Standard Time)

get Current date and time in New York
const now = new Date();
const options = { timeZone: 'America/New_York' };
const nyTime = now.toLocaleString('en-US', options);
console.log(nyTime); // Output: 4/2/2023, 6:14:57 PM

The options object in the example code specifies only the timeZone option. However, there are many other options available for formatting dates and times using the toLocaleString() method. Some of the commonly used options are:

weekday: A string specifying the display of the day of the week. Possible values are "narrow", "short", and "long".   const now = new Date();
  const options = { weekday: 'long' };
  const weekday = now.toLocaleString('en-US', options);
  console.log(weekday);
  // Output: Saturday

In this code, the weekday option is set to 'long', which means that the full name of the day of the week will be displayed. Other possible values for weekday are 'narrow' (abbreviated name) and 'short' (short name).

year: A string specifying the display of the year. Possible values are "numeric", "2-digit", and "long". const now = new Date();
const options = { year: 'numeric' };
const year = now.toLocaleString('en-US', options);
console.log(year);
// Output: 2023

In this code, the year option is set to 'numeric', which means that the year will be displayed as a four-digit number. Other possible values for year are '2-digit' (two-digit number) and 'long' (full year name).

month: A string specifying the display of the month. Possible values are "numeric", "2-digit", "narrow", "short", and "long". const now = new Date();
const options = { month: 'short' };
const month = now.toLocaleString('en-US', options);
console.log(month);
// Output: Apr

In this code, the month option is set to 'short', which means that the abbreviated name of the month will be displayed. Other possible values for month are 'numeric' (month number), '2-digit' (two-digit month number), 'narrow' (single-letter abbreviation), and 'long' (full month name).

day: A string specifying the display of the day of the month. Possible values are "numeric", "2-digit", and "numeric-leading-zero". const now = new Date();
const options = { day: 'numeric' };
const day = now.toLocaleString('en-US', options);
console.log(day);
// Output: 2

In this code, the day option is set to 'numeric', which means that the day of the month will be displayed as a number. Other possible values for day are '2-digit' (two-digit number) and 'numeric-leading-zero' (number with leading zero).

hour: A string specifying the display of the hour. Possible values are "numeric", "2-digit", and "numeric-leading-zero". const now = new Date();
const options = { hour: 'numeric' };
const hour = now.toLocaleString('en-US', options);
console.log(hour);
// Output: 10

In this code, the hour option is set to 'numeric', which means that the hour will be displayed as a number. Other possible values for hour are '2-digit' (two-digit number) and 'numeric-leading-zero' (number with leading zero).

minute: A string specifying the display of the minute. Possible values are "numeric", "2-digit", and "numeric-leading-zero". const now = new Date();
const options = { minute: '2-digit' };
const minute = now.toLocaleString('en-US', options);
console.log(minute);
// Output: 25

In this code, the minute option is set to '2-digit', which means that the minute will be displayed as a two-digit number. Other possible values for minute are 'numeric' (number) and 'numeric-leading-zero' (number with leading zero).

second: A string specifying the display of the second. Possible values are "numeric", "2-digit", and "numeric-leading-zero". const now = new Date();
const options = { second: 'numeric' };
const second = now.toLocaleString('en-US', options);
console.log(second);
// Output: 14

In this code, the second option is set to 'numeric', which means that the second will be displayed as a number. Other possible values for second are '2-digit' (two-digit number) and 'numeric-leading-zero' (number with leading zero).

hour12: A boolean value indicating whether to use a 12-hour clock (true) or a 24-hour clock (false). const now = new Date();
const options = { hour: 'numeric', hour12: true };
const hour12 = now.toLocaleString('en-US', options);
console.log(hour12); // Output: 10 AM

In this code, the hour option is set to 'numeric', which means that the hour will be displayed as a number. The hour12 option is set to true, which means that a 12-hour clock format will be used. If hour12 were set to false, a 24-hour clock format would be used instead.

SEARCH