//Date, Time, Timestamp
var today = new Date();
var DD = String(today.getDate()).padStart(2, '0');
var MM = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var YYYY = today.getFullYear();
var hh = today.getHours();
var mm = today.getMinutes();
var ss = today.getSeconds();
today = YYYY + MM + DD + hh + mm + ss;
console.log('Date-Time: ', today);
// Custom function to format date in following format
// dd-mm-yyyy
// dd/mm/yyyy
// dd.mm.yyyy
function dateFormater(date, separator) {
var day = date.getDate();
// add +1 to month because getMonth() returns month from 0 to 11
var month = date.getMonth() + 1;
var year = date.getFullYear();
// show date and month in two digits
// if month is less than 10, add a 0 before it
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
// now we have day, month and year
// use the separator to join them
return day + separator + month + separator + year;
}
var now = new Date();
console.log(dateFormater(now, '/'));
console.log(dateFormater(now, '-'));
console.log(dateFormater(now, '.'));
const d = new Date('2010-08-05')
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d)
const mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d)
const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d)
console.log(`${da}-${mo}-${ye}`)
let d = new Date(2010, 7, 5);
let ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
let mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d);
let da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);
console.log(`${da}-${mo}-${ye}`);
Run code snippet
function dateToYMD(date) {
var strArray=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var d = date.getDate();
var m = strArray[date.getMonth()];
var y = date.getFullYear();
return '' + (d <= 9 ? '0' + d : d) + '-' + m + '-' + y;
}
console.log(dateToYMD(new Date(2017,10,5))); // Nov 5
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
Run code snippetHide results
Modern JavaScript date utility library https://date-fns.org/
date-fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.