Date and Time Methods
Toby
Posted on February 9, 2023
The Date method is a very wide topic in JavaScript, and its use case can not be downplayed. In almost every JavaScript-related project you create, there is almost more than one reason to use date objects. When you create a timer on your website, or when you create a countdown clock for a sale, that's the date object in operation. The Date object has some parameters it works with such as milliseconds, seconds, minutes, hours, days, months and year. These parameters are passed into the date constructor to help finetune the return of our request. The Date Object is created with the new Date() keyword.
There are four (4) different ways to use the Date constructor:
new Date(): This method does not receive parameters. It just creates a Date object that outputs the current date and time.
const date = new Date()
returns Thu Feb 09 2023 01:24:20 GMT+0800 (China Standard Time)
new Date(milliseconds):
new Date(dataString):
new Date(year, month, date, hour, minute, second, millisecond): This method accepts the above-stated parameters, and returns them in the result. The parameters are discussed thus:
Β year: This returns an integer value representing the year in question e.g 2023.
monthIndex: Months in JavaScript are indexed like arrays, they start from 0. In that light, months start from 0β11.
date: This represents the current date.
hour: This represents the current hour. It is also 0 indexed, hence starting from 0β23.
minute: Represents the current minute
second: Represents the current second of the day
milliseconds: Represents the current millisecond.
Let's take a look at this example:
const date = new Date(2020, 9, 18, 23, 59, 59, 120)
returns Sun Oct 18 2020 23:59:59 GMT+0800 (China Standard Time)
Let's take a look at some of the most common other methods used in the Date object.
Using our newDate() keyword, we will be using the output to learn how other methods work. Please ride along with me.
const date = new Date()
returns // Wed Feb 08 2023 20:27:20 GMT+0100 (West Africa Standard Time)
getDate(): This returns the date.
const today = date.getDate() returns // 8
getDay(): This returns the day of the week.
const today = date.getDay() returns // 3
3 in this instance is Tuesday, and today is Wednesday. As I said earlier, Days in Date are 0 indexed, so to get today's day, we run the code like this:
const today = date.getDay() + 1.
This returns // 4, which is Wednesday.
getFullYear(): This returns the year in full.
const year = date.getFullYear(). This returns // 2023
getHours(): This returns the hour.
const hour = date.getHours(). This returns // 21. It's 9:07pm here in Nigeria.
getMilliseconds(): It returns the current milliseconds in the time. Let's try to catch the current milliseconds.
const milliseconds = date.getMilliseconds(). It returned // 337
getMinutes(): This returns the current minute in time.
const minute = date.getMinutes(). This returned // 21
getMonth(): It returns the current month at the time of writing this article.
const month = date.getMonth(). It returns // 1.
It returned 1 because months are 0 indexed. So to get the correct month, we do this:
const month = date.getMonth() + 1. This returns // 2
getSeconds(): This returns the current seconds of the day.
const seconds = date.getSeconds(). This returns // 35
getTime(): This returns the time of the day.
const time = date.getTime(). It returns // 1675888165218
If you think this was helpful, you can Follow me and like this article, so you can get updates when I write another article such as this.
Have a great time.
Posted on February 9, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.