A short introduction to javascript’s Date object
Javascript’s Date object and its method
Here is a short introduction to javascript’s Date Object. we will look at the Date object and some practical methods of it. Date object allows us to create, manipulate, and format dates.
Javascript stores date as a number of milliseconds since midnight at the beginning of January 1 1970 UTC ( Universal Time Coordinated ).
Constructor
Date() constructor is used to create a new Date object. when we call Date() as a function it returns a string that contains the current date and time.
const CurrentDateTime = Date(); // calling as function
console.log(CurrentDateTime);
// output : Wed Mar 22 2023 15:13:04 GMT+0530 (India Standard Time)
When we call Date as a constructor it returns a new Date object.
const CurrentDateTime = new Date(); // calling as constructor
console.log(CurrentDateTime);
// 2023-03-22T09:49:05.158Z
There are 9 different ways to create a Date object. here is an example of it.
year
integer value describes the year.month
integer value describes the month. where 0 is January and 11 is December.day
integer value describes the day of the month.hours
integer value between 0 to 23 describes the hours of the day.minutes
integer value describes the minutes of the date time.seconds
integer value describes the second of a time.milliseconds
integer value describes milliseconds of a time.
// new Date()
// new Date(date string)
// new Date(year,month)
// new Date(year,month,day)
// new Date(year,month,day,hours)
// new Date(year,month,day,hours,minutes)
// new Date(year,month,day,hours,minutes,seconds)
// new Date(year,month,day,hours,minutes,seconds,ms)
const a = new Date();
// Wed Mar 22 2023 15:31:48 GMT+0530 (India Standard Time)
const b = new Date("March 22, 2023 15:13:00");
// Wed Mar 22 2023 15:13:00 GMT+0530 (India Standard Time)
const c = new Date(2023, 2);
// Wed Mar 01 2023 00:00:00 GMT+0530 (India Standard Time)
const d = new Date(2023, 2, 2);
// Thu Mar 02 2023 00:00:00 GMT+0530 (India Standard Time)
const e = new Date(2023, 2, 22,1);
// 2023-03-21T19:30:00.000Z
const f = new Date(2023, 2, 22, 1,20);
// 2023-03-21T19:50:00.000Z
const g = new Date(2023, 2, 22, 1, 20, 10);
// 2023-03-21T19:50:10.000Z
const h = new Date(2023, 2, 22, 1, 20, 10,12);
// 2023-03-21T19:50:10.012Z
Useful Methods
Get Methods
getDate()
method returns the day of the month (0–31)
.
const a = new Date("March 22, 2023 15:13:00");
const day = a.getDate();
console.log(day); // 22
getDay()
method returns the day of the week (0–6)
where 0 is Sunday and 6 is Saturday.
const a = new Date("March 22, 2023 15:13:00");
const dayOfWeek = a.getDay();
console.log(dayOfWeek); // 3
getFullYear()
method returns the year. it is a four-digit number.
const a = new Date("March 22, 2023 15:13:00");
const fullYear = a.getFullYear();
console.log(fullYear); // 2023
getHours()
method returns hours for the specified date (0–23)
.
const a = new Date("March 22, 2023 15:13:00");
const hours = a.getHours();
console.log(hours); // 15
getMilliseconds()
method returns milliseconds for the specified date (0–999)
.
const a = new Date("March 22, 2023 15:13:20:10");
const milliSeconds = a.getMilliseconds();
console.log(milliSeconds); // 10
getMinutes()
method returns minutes for the specified date (0–59)
.
const a = new Date("March 22, 2023 15:13:20:10");
const minutes = a.getMinutes();
console.log(minutes); // 13
getMonth()
method returns the month for the specified date (0–11)
.
const a = new Date("March 22, 2023 15:13:20:10");
const month = a.getMonth();
console.log(month); // 2
getSeconds()
method returns the seconds for the specified date (0–59)
.
const a = new Date("March 22, 2023 15:13:20:10");
const seconds = a.getSeconds();
console.log(seconds); // 20
Set Methods
setDate()
method sets the day of the month of the date.
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getDate()); // 22
a.setDate(2);
console.log(a.getDate()); // 2
setFullYear()
method sets the year of the date.
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getFullYear()); // 2023
a.setFullYear(2019);
console.log(a.getFullYear()); // 2019
setHours()
method sets the hour of the date.
// we have different setHours method like
// setHours(hours);
// setHours(hours,minutes);
// setHours(hours, minutes, seconds);
// setHours(hours, minutes, seconds, ms);
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getHours()); // 15
a.setHours(10);
console.log(a.getHours()); // 10
setMilliseconds()
method sets the milliseconds of the date.
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getMilliseconds()); // 10
a.setMilliseconds(256);
console.log(a.getMilliseconds()); // 256
setMinutes()
method sets the minutes of the date.
// we have different setMinutes method like
// setMinutes(minutes);
// setMinutes(minutes, seconds);
// setMinutes(minutes, seconds, ms);
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getMinutes()); // 13
a.setMinutes(20);
console.log(a.getMinutes()); // 20
setMonth()
method sets the month of the date.
// we have different setMonth method like
// setMonth(month);
// setMonth(month, day);
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getMonth()); // 2
a.setMonth(8);
console.log(a.getMonth()); // 8
setSeconds()
method sets the seconds of the date.
// we have different setSeconds method like
// setSeconds(seconds);
// setSeconds(seconds, ms);
const a = new Date("March 22, 2023 15:13:20:10");
console.log(a.getSeconds()); // 20
a.setSeconds(26);
console.log(a.getSeconds()); // 26
This is just a short introduction to javascript date Object. if you want to know more about javascript’s Date object then check out these docs.
Thank You!