get current datetime in javascript code example
Example 1: return current date in javascript
let today = new Date().toLocaleDateString()
console.log(today)
Example 2: today's time in javascript
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
Example 3: javascript get current time
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
Example 4: javascript current date time
var today = new Date().toLocaleDateString(undefined, {
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})
Example 5: how to print date like 10-may-2018 in javascript
date.toString('YYYY-MM-dd'); 'Tue Feb 10 2015 15:42:50'
var date = new Date('4-1-2015');
date.getDay();
date.getYear();
date.getFullYear();
date.getMonth();
date.getUTCDate();
Example 6: javascript date methods
var d= new Date();
d.getFullYear();
d.getMonth();
d.getDate();
d.getHours();
d.getMinutes();
d.getSeconds();
d.getMilliseconds();
d.getTime();