简体   繁体   中英

How to convert a date to long date?

How to convert a date(01-02-2019) to Wed, 02 Jan 2019 in javascript?

 $(document).ready(function () {
       var dealDate = 01-02-2019;
});

Just use new Date() on that date value:

 $(document).ready(function() { var dealDate = '01-02-2019'; //replace all - to / to make it work on firefox dealDate = dealDate.replace(/-/g,'/'); alert(new Date(dealDate).toDateString()) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You can use Date Constructor and Date.prototype.toDateString() :

The toDateString() method returns the date portion of a Date object in human readable form in American English.

 $(document).ready(function () { var dealDate = new Date('01-02-2019').toDateString(); console.log(dealDate); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You can split your string on - and then generate the date using Date constructor.

 var dealDate = '01-02-2019'; let [month, day, year] = dealDate.split('-').map(Number); let date = new Date(year, month - 1, day); console.log(date.toDateString()); 

TO convert the date to in the format of month day, month day number and year use the below jquery. It will convert the current date to the exact format you asked for

$(document).ready(function() {
var dealDate = new Date();
alert(dealDate.toUTCString());
});

your expected format is like [day][comma][date][month][year]. I split toDateString() and rearranged in expected format.

  function formatedDate(d){ var dt=new Date(d).toDateString().split(' '); return dt[0]+', '+dt[2]+' '+dt[1]+' '+dt[3]; //[Day][comma][date][month][year] } console.log(formatedDate('01-02-2019')) 

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