JavaScript Date Methods

JavaScript Date Methods

Get Date Methods :

getFullYear()

Get the year as a four digit number (yyyy)

const year = new Date(); year.getFullYear(); // Current year // 2022

getMonth()

Get the month as a number (0-11)

//To get the correct month, you must add 1: const month = new Date(); month.getMonth() +1; // In JavaScript, the first month (January) is month number 0, so December returns month number 11.

getDate()

Get the day as a number (1-31)

const date = new Date(); date.getDate(); // Current date // 17

getHours()

Get the hour (0-23)

const hour = new Date(); hour.getHours(); // Current hour // 10

getMinutes()

Get the minute (0-59)

const minute = new Date(); minute.getMinutes(); // Current minute // 54

getTime()

Get the time (milliseconds since January 1, 1970)

const time = new Date(); time.getTime(); // the number of milliseconds since January 1, 1970 // 1652765189344

getDay()

Get the weekday as a number (0-6)

const day = new Date(); day.getDay(); // Current day since Sunday (0) // 2 // In JavaScript, the first day of the week (0) means "Sunday", even if some countries in the world consider the first day of the week to be "Monday"

Date.now()

Get the time. ECMAScript 5.

let d = Date.now(); // console.log(d) // 1652962707372

getSeconds()

Get the second (0-59)

const second = new Date(); second.getSeconds(); // current second 33

getMilliseconds()

Get the millisecond (0-999)

const ms = new Date(); ms.getMilliseconds(); // Current millisecond : // 200

UTC Date Methods :

getUTCDate()

Same as getDate(), but returns the UTC date

getUTCDay()

Same as getDay(), but returns the UTC day

getUTCFullYear()

Same as getFullYear(), but returns the UTC year

getUTCMonth()

Same as getMonth(), but returns the UTC month

getUTCHours()

Same as getHours(), but returns the UTC hour

getUTCMinutes()

Same as getMinutes(), but returns the UTC minutes

getUTCSeconds()

Same as getSeconds(), but returns the UTC seconds

getUTCMilliseconds()

Same as getMilliseconds(), but returns the UTC milliseconds

Set Date Methods :

setDate()

Set the day as a number (1-31)

const date = new Date(); date.setDate(1); console.log(date) // Sun May 01 2022 17:21:00 GMT+0530 (India Standard Time)

setFullYear()

Set the year (optionally month and day)

const date = new Date(); date.setFullYear(2022); // console.log(date) // Thu May 19 2022 17:22:42 GMT+0530 (India Standard Time)

setMonth()

Set the month (0-11)

const date = new Date(); date.setMonth(4); // console.log(date) // Wed Jan 19 2022 17:25:23 GMT+0530 (India Standard Time)

setHours()

Set the hour (0-23)

const date = new Date(); date.setHours(15); // console.log(date) // Thu May 19 2022 👉 15:27:37 GMT+0530 (India Standard Time)

setMinutes()

Set the minutes (0-59)

const date = new Date(); date.setMinutes(1); // console.log(date) // Thu May 19 2022 17:01:35 GMT+0530 (India Standard Time)

setSeconds()

Set the seconds (0-59)

const date = new Date(); date.setSeconds(35); // console.log(date) // Thu May 19 2022 17:33:35 GMT+0530 (India Standard Time)

setMilliseconds()

Set the milliseconds (0-999)

const date = new Date(); date.setMilliseconds(192); // console.log(date) // Thu May 19 2022 17:34:09 GMT+0530 (India Standard Time)

setTime()

Set the time (milliseconds since January 1, 1970)

const date = new Date(); date.setTime(1332403882588); // console.log(date) // Thu Mar 22 2012 13:41:22 GMT+0530 (India Standard Time)

new Date()

Creates a new date object with the current date and time.

const d = new Date(); // console.log(d) // Thu May 19 2022 17:42:16 GMT+0530 (India Standard Time)

new Date(year, month, …)

creates a new date object with a specified date and time.

const d = new Date(2022, 4, 19, 10, 33, 30, 0); // console.log(d) // Thu May 19 2022 10:33:30 GMT+0530 (India Standard Time)

new Date(dateString)

creates a new date object from a date string.

const d = new Date("October 13, 2014 11:13:00"); // console.log(d) // Mon Oct 13 2014 11:13:00 GMT+0530 (India Standard Time)

new Date(milliseconds)

creates a new date object as zero time plus milliseconds.

const d = new Date(100000000000); // console.log(d) // Sat Mar 03 1973 15:16:40 GMT+0530 (India Standard Time)

I hope you like this article.

Also Read :