简体   繁体   中英

How to change Date format in JavaScript?

I want to change a date's format in JavaScript. I tried

 var today = new Date();
 today.toLocaleFormat('%d-%b-%Y');

but that didn't work. How can I approach this problem?

Please find below my answer ,

var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth()+1; //January is 0! 
var yyyy = today.getFullYear(); 
if(dd<10)
{
    dd='0'+dd
} 
if(mm<10)
{  
    mm='0'+mm 
} 
var today = dd+'/'+mm+'/'+yyyy; 

I think there is no straight way to do so. Let's check these out:

var date = new Date();
var options = {
    weekday: "long", year: "numeric", month: "short",
    day: "numeric", hour: "2-digit", minute: "2-digit"
};

//alert(date.toLocaleDateString("en-US"));
alert(date.toLocaleTimeString("en-us", options));

And I think you are looking for this:

var myDate = new Date();
alert(myDate.getDate()  + "-" + (myDate.getMonth() + 1)+ "-" + myDate.getFullYear());

Yes, I've googled and got this solution. Please check it out. :) Thanks!

date = new Date();
date.format('dd st-MMM-yyyy'); 

Here's an article that explains it in depth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM