var options ={weekday:'long',year:'numeric',month:'long',day:'numeric'};var today =newDate();console.log(today.toLocaleDateString("en-US"));// 9/17/2016console.log(today.toLocaleDateString("en-US", options));// Saturday, September 17, 2016// For custom format use
date.toLocaleDateString("en-US",{day:'numeric'})+"-"+ date.toLocaleDateString("en-US",{month:'short'})+"-"+ date.toLocaleDateString("en-US",{year:'numeric'})// 16-Nov-2019
// Many options with Intl.DateTimeFormatconst formatter =newIntl.DateTimeFormat('en',{hour12:true,hour:'numeric',minute:'2-digit',second:'2-digit'});
formatter.format(newDate());
// Custom function to format date in following format// dd-mm-yyyy// dd/mm/yyyy// dd.mm.yyyyfunctiondateFormater(date, separator){var day = date.getDate();// add +1 to month because getMonth() returns month from 0 to 11var 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 itif(day <10){
day ='0'+ day;}if(month <10){
month ='0'+ month;}// now we have day, month and year// use the separator to join themreturn day + separator + month + separator + year;}var now =newDate();console.log(dateFormater(now,'/'));console.log(dateFormater(now,'-'));console.log(dateFormater(now,'.'));
const d =newDate('2010-08-05')const ye =newIntl.DateTimeFormat('en',{year:'numeric'}).format(d)const mo =newIntl.DateTimeFormat('en',{month:'short'}).format(d)const da =newIntl.DateTimeFormat('en',{day:'2-digit'}).format(d)console.log(`${da}-${mo}-${ye}`)
let d =newDate(2010,7,5);let ye =newIntl.DateTimeFormat('en',{year:'numeric'}).format(d);let mo =newIntl.DateTimeFormat('en',{month:'short'}).format(d);let da =newIntl.DateTimeFormat('en',{day:'2-digit'}).format(d);console.log(`${da}-${mo}-${ye}`);Run code snippet
functiondateToYMD(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(newDate(2017,10,5)));// Nov 5
functionformatDate(date){var d =newDate(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
ModernJavaScript 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.