DEV Community

Cover image for Date & Time in JavaScript
Himanshu Tiwari 🌼
Himanshu Tiwari 🌼

Posted on

Date & Time in JavaScript

Let’s meet someone amazing today 😁, haha a new built-in object: Date. In Javascript, we can easily work with Date & Time using the Date class. To create an object of this type we use the new keyword.

 // Creating a Date object
    var dateObj = new Date();

    console.log(dateObj);
Enter fullscreen mode Exit fullscreen mode

By doing this, we create an object with the current date and time formed by weekday, month, day, year, hours, minutes, seconds, and timezone. Now that's friggin precise 😝.

Methods of the Date object 😡

Method Description
getDate() Returns the day of the month (1 to 31)
getDay() Returns the weekday (0 - Sunday to 6 -
Saturday)
getFullYear() Returns the full year (YYYY)
getMonth() Returns the month (0 to 11)
getHours() Returns the hours (0 to 23)
getMinutes() Return the minutes (0 to 59)
getSeconds() Returns the seconds (0 to 59)
getMilliseconds() Returns the milliseconds (0 to 999)
getTime() Returns the number of milliseconds since the
Epoch(Jan 1st, 1970, 00:00:00)
setTime() Creates a specific date from milliseconds
since the epoch.
What is UNIX EPOCH?😰

The Epoch is present in most programming languages. We can consider it the starting point of counting the time. This is useful to do calculations with date and time.

Javascript uses the Unix Epoch, which is: Jan 1st, 1970, 00:00:00, UTC.

A Time Chart for reference πŸ˜…

Time Milliseconds (ms)
1 second 1,000
1 minute 60,000
1 hour 3,600,000
1 day 86,400,000
1 year (365 days) 31,536,000,000
How to calculate how many hours have passed since the Epoch? 😬
    var Obj = new Date();

    Obj = Obj.getTime();

    var hours = Obj / 3600000;

    console.log(Math.floor(hours));
Enter fullscreen mode Exit fullscreen mode

Passing Argument to Date() 😷

We created date objects without passing any arguments, this is why they represented the current time but we can also use them with specific objects or better to say a specific number of objects.

Numeric arguments Time represents
One milliseconds since the epoch
Two years and month (months go from 0 to 11)
Three years, month, and day
Four years, month, day, and hours
Five years, month, days, hours, minutes
Six years, month, day, hours, minutes, and
var Obj = new Date(2021,1,24);
    console.log( Obj );
Enter fullscreen mode Exit fullscreen mode

We can also pass strings as an argument and in different formats

  • ISO date: "2020-03-18" (YYYY-MM-DD)
  • Short Date: "03/18/2020" (MM/DD/YYYY)
  • Long Date: "Mar 18 2020" or "18 Mar 2020"

I hope you guys have learnt and saved it for later reference
and if you found my grammatical mistakes then don't forget to comment them πŸ˜‚

Oldest comments (8)

Collapse
 
yamini8750 profile image
Yamini-8750 • Edited

Now this is something really useful bro😎

Collapse
 
himanshutiwari15 profile image
Himanshu Tiwari 🌼

πŸ˜‡thanks

Collapse
 
galabra profile image
galabra

Nicely put. However one of the most important skills for beginners is to be able to read documentation, such as this

Collapse
 
himanshutiwari15 profile image
Himanshu Tiwari 🌼

Well, when I started reading MDN Docs, I was like what the hell, who puts so much things all together, hyperlink after hyperlink... But then after time, I understood that not every hyperlink is meant to be opened, what you came for is important and all you have to do is just stick to that

You said right
It's so important for beginners to understand

Collapse
 
marius profile image
Marius Espejo

regarding the other comments:

I think it's good to write technical things down as you've personally understood it, even if it is already documented somewhere. If everyone relied only on docs, then who would be blogging?

anyways nice work, you might want to also check out libraries like date-fns to further help you with working with dates.

Collapse
 
himanshutiwari15 profile image
Himanshu Tiwari 🌼

So glad to see such a generous person on the comments
Thanks
And definitely will check that
And if I learnt how to use them and work with them, then will write about them also lol πŸ˜‚

Some comments may only be visible to logged-in visitors. Sign in to view all comments.