Shravani Thirunagari
Shravani Roy


Shravani Roy


Date - An important module of javascript.

Shravani Thirunagari's photo
Shravani Thirunagari
·Oct 7, 2022·

3 min read

Everything in real-time deals with timestamps and there is a maximum chance that you could resolve a problem using timestamps. Starting from bank transactions, appointments, check-in/checkout time, and whatnot. So, javascript provides an inbuilt Date prototype, which could be used to manipulate the Date objects and play around to get the required output. This blog is to focus on the various methods of a Date object, available that are used on a day-to-day basis in developing projects. Let us get started.

Creating a Date object.

We could create a date object using the new keyword as shown below.

const todayDate = new Date();
console.log("Today's date is ", todayDate);

So, from the above code snippet, when the Date() constructor is called, it returns a Date object.

Screenshot 2022-10-07 at 4.52.31 PM.png

It totally depends on the requirement of which method to use. Whichever method makes your life easy :)

  1. getDate() - returns the day of the month, of the date passed.
  2. getFullYear() - returns the date year in YYYY format, of the date passed
  3. getMonth() - returns the month of the date passed, in numeric format.
  4. getHours() - returns the number of hours of the date passed, according to universal time.
  5. getMinutes() - returns the minutes of the date passed, according to universal time.
  6. getSeconds() - returns the seconds of the date passed, according to universal time.
  7. getMilliSeconds() - returns the milliseconds of the date passed, according to universal time.

There are many more instance methods that you can check here.

There are some set methods that could be used to set the date in different formats based on requirements. These set methods are based on local time and the one it was initially created. Let me list a few popular set methods below.

  1. setDate()
  2. setUTCDate()
  3. setUTCMilliseconds()

Also, I have shared a few methods below, that I have used often in my development journey.

  1. toLocaleString() - returns a string with locality-sensitive (reads from system clock) representation of the date. This method accepts some interesting attributes too.
  2. toDateString() - returns the date portion of the date in string format.
  3. toTimeString() - returns the time portion of the date as a string.
  4. toUTCString() - returns the date in string format based on UTC timezone.

You might be wondering why we need so many methods to convert date objects into strings of different formats. Well, it makes it easy for us to retrieve human-understandable strings and for developers, it is easy to play/manipulate the strings.

Real-time usecase:

Yesterday, someone reached out to me asking if I could help them solve a date issue in javascript that needs to be passed in Bhoomi. He said that the script would receive the in-time of a student in the class and the duration he has attended the class. The expected output is the out time of the student. This is pretty straight forward right? Well yes, and I gave the below script.

const getOutTime = (inTime, duration) => {
    const inTimeMS = new Date(inTime).getTime();
    const durationMS = parseInt(duration) * 60 * 60 * 1000;
    const outTime = new Date(inTimeMS + durationMS); //this is in MS(milli seconds)
    return new Date(outTime).toLocaleString(); 

const output = getOutTime('9/26/2022 7:20', 2.5);

Run this on your browser console and pass different arguments to getOutTime() to check the output. I have a couple of other assignments for you for the above function.

  1. You could slice the seconds from the output date-time string, and get the format - 'DD/MM/YYYY HH:MM'
  2. Try to set the time of the in-time of students in a specific time-zone with and without using the timeZone attribute of the toLocaleString method.

I hope this was helpful. Thank you! Happy learning.

Share this