Date - An important module of javascript.
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.
Some of the popular instance methods.
It totally depends on the requirement of which method to use. Whichever method makes your life easy :)
- getDate() - returns the day of the month, of the date passed.
- getFullYear() - returns the date year in YYYY format, of the date passed
- getMonth() - returns the month of the date passed, in numeric format.
- getHours() - returns the number of hours of the date passed, according to universal time.
- getMinutes() - returns the minutes of the date passed, according to universal time.
- getSeconds() - returns the seconds of the date passed, according to universal time.
- 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.
- setDate()
- setUTCDate()
- setUTCMilliseconds()
Also, I have shared a few methods below, that I have used often in my development journey.
- toLocaleString() - returns a string with locality-sensitive (reads from system clock) representation of the date. This method accepts some interesting attributes too.
- toDateString() - returns the date portion of the date in string format.
- toTimeString() - returns the time portion of the date as a string.
- 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);
console.log(output);
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.
- You could slice the seconds from the output date-time string, and get the format - 'DD/MM/YYYY HH:MM'
- 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.