Reputation: 307
How to convert a date(01-02-2019) to Wed, 02 Jan 2019 in javascript?
$(document).ready(function () {
var dealDate = 01-02-2019;
});
Upvotes: 1
Views: 86
Reputation: 99
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'))
Upvotes: 0
Reputation: 68933
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>
Upvotes: 1
Reputation: 22524
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());
Upvotes: 1
Reputation: 317
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());
});
Upvotes: 0
Reputation: 30739
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>
Upvotes: 2