var defaultdatetime = new Date(); console.log(defaultdatetime);
Wed Apr 05 2017 07:13:59 GMT+0545 (NPT)
We can display a datetime in more human readable using this method.
var d = new Date(); var formatted = d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes(); console.log(formatted);
Here’s a result.
We have to remember that getMonth() method returns month between 0 to 11, so we have to add 1 to match current month.
Example to show full date with day name.
var dayname = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var d = new Date(); var formatted = dayname[d.getDay()]+", "+d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes(); console.log(formatted);
Wednesday, 5-4-2017 7:13
Example to show date with month name.
var monthname = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; var d = new Date(); var formatted = monthname[d.getMonth()]+" "+d.getDate()+", "+d.getFullYear(); console.log(formatted);
April 5, 2017