DEV Community

loading...
Cover image for πŸ“… Master Javascript Dates In 2 Minutes

πŸ“… Master Javascript Dates In 2 Minutes

satishnaikawadi2001 profile image Satish Naikawadi Originally published at satishnaikawadi.me ・3 min read

In this article , we will learn all the important topics related to dates in javascript with help of proper examples.
Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

Creating A Date Object

We can create a Date object using Date() constructor with the following syntaxes -

new Date() //current date and time as of the time of instantiation.
new Date(value) // value is an integer value representing the number of milliseconds since January 1, 1970, 00:00:00
new Date(dateString) // A string value representing a date
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]) //Give at least a year and month
Enter fullscreen mode Exit fullscreen mode

Examples

let today = new Date();
console.log(today);
let myDay = new Date('January 16, 2001 05:39:00');
console.log(myDay);
myDay = new Date('2001-01-16T05:39:00');
console.log(myDay);
myDay = new Date(2001, 0, 16); // the month is 0-indexed
console.log(myDay);
myDay = new Date(1995, 0, 16, 5, 39, 0);
console.log(myDay);
Enter fullscreen mode Exit fullscreen mode

Output

2021-04-12T19:46:14.180Z
2001-01-16T00:09:00.000Z
2001-01-16T00:09:00.000Z
2001-01-15T18:30:00.000Z
1995-01-16T00:09:00.000Z

Important Instance Methods

getDate()

Returns the day of the month (1–31) for the specified date according to local time.

console.log(myDay.getDate());
Enter fullscreen mode Exit fullscreen mode

Output

16

getDay()

Returns the day of the week (0–6) for the specified date according to local time.

console.log(myDay.getDay());
Enter fullscreen mode Exit fullscreen mode

Output

1

getFullYear()

Returns the year (4 digits for 4-digit years) of the specified date according to local time.

console.log(myDay.getFullYear());
Enter fullscreen mode Exit fullscreen mode

Output

1995

getHours()

Returns the hour (0–23) in the specified date according to local time.

console.log(myDay.getHours());
Enter fullscreen mode Exit fullscreen mode

Output

5

getMinutes()

Returns the minutes (0–59) in the specified date according to local time.

console.log(myDay.getMinutes());
Enter fullscreen mode Exit fullscreen mode

Output

39

getSeconds()

Returns the seconds (0–59) in the specified date according to local time.

console.log(myDay.getSeconds());
Enter fullscreen mode Exit fullscreen mode

Output

0

getMonth()

Returns the month (0–11) in the specified date according to local time.

console.log(myDay.getMonth());
Enter fullscreen mode Exit fullscreen mode

Output

0

We also have similar methods with UTC as getUTCDate(),getUTCDay(),getUTCFullYear(),getUTCHours(),getUTCMilliseconds(),getUTCMinutes(),getUTCMonth() and getUTCSeconds() which will give similar results but according to universal time.

We also have similar setter methods with local time as well as with UTC which are setUTCDate(),setUTCDay(),setUTCFullYear(),setUTCHours(),setUTCMilliseconds(),setUTCMinutes(),setUTCMonth() and setUTCSeconds() which will set the particular parameters according to universal time. While setDate(),setDay(),setFullYear(),setHours(),setMilliseconds(),setMinutes(),setMonth() and setSeconds() will set the parameters according to lacale time.

Important Methods To Convert Date To String

toDateString()

Returns the "date" portion of the Date as a human-readable string like Tue Apr 13 2021.

toISOString()

Converts a date to a string following the ISO 8601 Extended Format like1995-01-16T00:09:00.000Z .

toUTCString()

Converts a date to a string using the UTC timezone like Mon, 16 Jan 1995 00:09:00 GMT.

toLocaleString()

Returns a string with a locality-sensitive representation of this date like 1/16/1995, 5:39:00 AM.

Calculating Elapsed Time

let start = new Date()
// The event to time goes here:
doSomethingForALongTime()
let end = new Date()
let elapsed = end.getTime() - start.getTime() // elapsed time in milliseconds
Enter fullscreen mode Exit fullscreen mode

Get the number of seconds since the ECMAScript Epoch

let seconds = Math.floor(Date.now() / 1000)
Enter fullscreen mode Exit fullscreen mode

So , that is it for this article πŸ˜€. I hope you understand how to manipulate dates in javascript. You can visit my Personal Blog for more articles related to programming. And lastly as always , Thank You for reading.

Discussion (1)

Collapse
devlorenzo profile image
Collapse
bymax00x profile image
Runal Manandhar

using moment is efficient

Forem Open with the Forem app