javascript get date
// Get today's date in Javascript
var today = new Date();
// Get year
var year = today.getFullYear();
// Get month
var month = today.getMonth();
// Get day
var day = today.getDate();
// Get milliseconds since 1 January 1970
var milliseconds = today.getTime();
// Get timezone offset (in minutes)
var timezoneOffset = today.getTimezoneOffset();
// Get day of week (0 to 6)
var dayOfWeek = today.getDay();
// Format date
// Day/Month/Year
var date = day + "/"+ (month + 1) + "/" + year;
// Output
console.log(date);
The above code will output the current date in the format of Day/Month/Year. For example, if today is December 21, 2020, it will output 21/12/2020.
You can also use the toDateString()
method to get a more readable date. For example, the following code will output Monday, December 21, 2020:
// Get date
var date = today.toDateString();
// Output
console.log(date);
You can also get the hour, minute, and second of the current time in Javascript by using the getHours()
, getMinutes()
, and getSeconds()
methods. For example, the following code will output the current hour, minute, and second:
// Get hour
var hour = today.getHours();
// Get minute
var minute = today.getMinutes();
// Get second
var second = today.getSeconds();
// Output
console.log(hour + ":" + minute + ":" + second);
You can also use the toTimeString()
method to get a more readable time. For example, the following code will output 3:37:21 PM:
// Get time
var time = today.toTimeString();
// Output
console.log(time);