You can use the built-in Date object in JavaScript to obtain the current date and time.
To obtain the current date and time, you can create a new Date object without any parameters:
var now = new Date();
This will create a Date object representing the current date and time.
You can then use various methods of the Date object to obtain the individual components of the current date and time, such as:
getYear()
or getFullYear()
: returns the yeargetMonth()
: returns the month (0-11)getDate()
: returns the day of the month (1-31)getDay()
: returns the day of the week (0-6)getHours()
: returns the hour (0-23)getMinutes()
: returns the minute (0-59)getSeconds()
: returns the second (0-59)getMilliseconds()
: returns the millisecond (0-999)For example, to obtain the current year, you can use:
var now = new Date();
var year = now.getFullYear();
Similarly, to obtain the current hour, you can use:
var now = new Date();
var hour = now.getHours();
Note that all the methods of the Date object return values in the local time zone of the computer running the JavaScript code. If you need to work with specific time zones or date formats, you may want to use external libraries or APIs.
Asked: 2022-02-19 11:00:00 +0000
Seen: 7 times
Last updated: Jul 28 '21