In JavaScript, you can get the current date and time using the built-in Date
object. The Date
object represents a single moment in time in a platform-independent format. The date and time information can be accessed and manipulated using various methods provided by the Date
object.
To get the current date and time, you can simply create a new instance of the Date
object without any arguments. This will create a Date
object with the current date and time based on the system clock of the user’s computer.
const now = new Date();
console.log(now); // output: Tue May 09 2023 09:00:03 GMT+0530 (India Standard Time)
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then log the value of now
to the console, which will output the current date and time in the format shown above.
Alternatively, you can use the Date.now()
method to get the current date and time as the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).
const now = Date.now();
console.log(now); // output: 1683602981359
In the code above, we call the Date.now()
method to get the current date and time as the number of milliseconds since the Unix epoch. We assign the result to a variable called now and log its value to the console.
To format the date and time in a specific way, you can use the various methods provided by the Date
object. For example, you can use the toLocaleString()
method to format the date and time according to the user’s locale.
const now = new Date();
console.log(now.toLocaleString()); // output: 5/9/2023, 2:30:22 PM
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then call the toLocaleString()
method on the now
object to format the date and time according to the user’s locale. We log the formatted date and time to the console.
You can also use the getFullYear()
, getMonth()
, getDate()
, getHours()
, getMinutes()
, and getSeconds()
methods to get specific parts of the date and time.
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1; // add 1 because getMonth() returns 0-based index
const date = now.getDate();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
console.log(`${year}-${month}-${date} ${hours}:${minutes}:${seconds}`); // output: 2023-5-9 14:30:22
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then use the various methods provided by the Date
object to get the year, month, date, hours, minutes, and seconds of the current date and time. We format these values into a string and log it to the console.
You can also use the getTimezoneOffset()
method to get the difference between the user’s local time and UTC time in minutes.
const now = new Date();
const offsetInMinutes = now.getTimezoneOffset();
console.log(offsetInMinutes); // output: -420 (for Pacific Daylight Time)
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then call the getTimezoneOffset()
method on the now
object to get the difference between the user’s local time and UTC time in minutes. We assign the result to a variable called offsetInMinutes
and log its value to the console.
You can also use the toLocaleDateString()
and toLocaleTimeString()
methods to get the formatted date and time strings separately.
const now = new Date();
const date = now.toLocaleDateString();
const time = now.toLocaleTimeString();
console.log(`${date} ${time}`); // output: 5/9/2023 2:30:22 PM
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then use the toLocaleDateString()
and toLocaleTimeString()
methods to get the formatted date and time strings separately. We assign the results to variables called date
and time
, respectively. We then concatenate these variables into a single string and log it to the console.
You can also use the UTC
methods provided by the Date
object to get the date and time in Coordinated Universal Time (UTC).
const now = new Date();
const utcDate = now.getUTCDate();
const utcMonth = now.getUTCMonth() + 1; // add 1 because getUTCMonth() returns 0-based index
const utcYear = now.getUTCFullYear();
const utcHours = now.getUTCHours();
const utcMinutes = now.getUTCMinutes();
const utcSeconds = now.getUTCSeconds();
console.log(
`${utcYear}-${utcMonth}-${utcDate} ${utcHours}:${utcMinutes}:${utcSeconds}`
); // output: 2023-5-9 21:30:22 (for Pacific Daylight Time)
In the code above, we create a new instance of the Date
object and assign it to a variable called now
. We then use the UTC methods provided by the Date
object to get the date and time in Coordinated Universal Time (UTC). We assign these values to variables called utcDate
, utcMonth
, utcYear
, utcHours
, utcMinutes
, and utcSeconds
, respectively. We format these values into a string and log it to the console.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.