Current I have this var called uniqueDate
and If I try to get the value of this variable console.log(uniqueDate);
This will give me a log in inspect element like this
["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"]
In php there's date('M j, Y',strtotime(<data>))
that will result something like this
May 24, 2020
May 25, 2020
May 26, 2020
May 27, 2020
May 28, 2020
May 29, 2020
I want to convert the date format of var uniqueDate
to M j, Y
like the sample date above.
Using Jquery? or just JS
Current I have this var called uniqueDate
and If I try to get the value of this variable console.log(uniqueDate);
This will give me a log in inspect element like this
["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"]
In php there's date('M j, Y',strtotime(<data>))
that will result something like this
May 24, 2020
May 25, 2020
May 26, 2020
May 27, 2020
May 28, 2020
May 29, 2020
I want to convert the date format of var uniqueDate
to M j, Y
like the sample date above.
Using Jquery? or just JS
Share Improve this question edited May 13, 2020 at 2:39 John Conde 220k99 gold badges463 silver badges502 bronze badges asked May 13, 2020 at 2:26 PabloPablo 1,4351 gold badge15 silver badges52 bronze badges 1- 1 Use a date library like moment.js or date-fns – charlietfl Commented May 13, 2020 at 2:32
6 Answers
Reset to default 3You can try with this:
var date = new Date('2020-05-24');
var normalizedDate = new Date(date.getTime() - date.getTimezoneOffset() * -60000);
var formattedDate = normalizedDate.toLocaleDateString('en-US', {
day: 'numeric',
month: 'short',
year: 'numeric'
});
console.log(formattedDate);
Hope it helps.
var dates = ["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"]
var data = [];
for (var date of dates)
{
date = new Date(date);
var final = date.toDateString();
var final = final.substring(4, 15);
data.push(final);
}
console.log(data);
With this line of code 'var final = final.substring(4, 15);' The result is as following
[ 'May 24 2020',
'May 25 2020',
'May 26 2020',
'May 27 2020',
'May 28 2020',
'May 29 2020' ]
Without this line of code 'var final = final.substring(4, 15);' it will be as following
[ 'Sun May 24 2020',
'Mon May 25 2020',
'Tue May 26 2020',
'Wed May 27 2020',
'Thu May 28 2020',
'Fri May 29 2020' ]
I'm a big fan of moment.js if you're working a lot with dates, but for simple formatting, this is about as close as you'll get to the PHP way:
let uniqueDate = [ // better to call it uniqueDates
"2020-05-24",
"2020-05-25",
"2020-05-26",
"2020-05-27",
"2020-05-28",
"2020-05-29"
]
let formatedDates = uniqueDate.map((arr) => {
let date = new Date(arr)
var options = { month: "short", day: "numeric", year: "numeric" }
return new Intl.DateTimeFormat("en-US", options).format(date)
})
console.log(formatedDates)
// ["May 23, 2020", "May 24, 2020", "May 25, 2020", "May 26, 2020", "May 27, 2020", "May 28, 2020"]
Here's the fiddle: https://jsfiddle/x3dymjh9/
It's pretty simple. Just create a new Javascript Date object and call toDateString() method from that object. If you do not need the day of week, just call the substring method
String.prototype.insert = function(index,str){
return this.slice(0,index) + str + this.slice(index)
}
var a = new Date('2020-05-27');
console.log(a.toDateString());
console.log(a.toDateString().substring(4));
console.log(a.toDateString().substring(4).insert(-5, ','));
I have done this by creating months
object. we can even customize it when it es to different months than English months.
const list = ["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"];
const monthObj = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December",
}
const formattedList = list.map((item, index) => {
const arr = item.split('-');
const year = arr[0];
const month = arr[1];
const day = arr[2];
return monthObj[month] + " " + day +
", " + year;
})
console.log(formattedList)
pure JS code,
- juste use the function named convertDate
const convertDate = dx => (new Date(dx)).toLocaleDateString('en-US', { month: 'short', day: '2-digit', year: 'numeric' });
for (let dt of ["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"])
{
document.write( convertDate(dt) , '<br>')
}