Reputation: 3159
I have an array of dates like:
dates=['2nd Oct 2014','20th Oct 2014','20th May 2019']
and I want to return them as an array in the YYYY-MM-DD format. so far I have this:
function formatDate(dates) {
var t=[];
dates.map(function(date){
var d = new Date(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;
t.push([year, month, day].join('-'));
});
console.log(t);
return t;
}
var dates = ['2nd Oct 2014','20th Oct 2014','20th May 2019']
console.log(formatDate(dates));
but this gives me :
[NaN-NaN-NaN,NaN-NaN-NaN,NaN-NaN-NaN]
since its not able to recognize the date with "th", "nd".any idea how to fix this so that I get the o/p as:
[2014-10-02,2014-10-20,2019-05-20]
here is the fiddle: http://jsfiddle.net/nakpchvf/
Thanks!
Upvotes: 2
Views: 262
Reputation: 3408
Just remove letters.
function formatDate(dates) {
var t=[];
dates.map(function(date){
let datePieces = date.split(' ');
datePieces[0] = datePieces[0].replace(/\D+/g, '');
var d = new Date(datePieces.join(' ')),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
t.push([year, month, day].join('-'));
});
return t;
}
var dates = ['2nd Oct 2014','20th Oct 2014','20th May 2019']
console.log(formatDate(dates));
Basically above I take each date and split it into 3 different parts. I then fix the first index (the one causing an issue) by removing any non-numerical characters. After that I can recombine to form your date string :).
Upvotes: 1
Reputation: 3040
Just use replace() function to replace these words with empty like this
function formatDate(dates) {
var t=[];
dates.map(function(date){
var words=["th","nd"];
for(var i=0;i<words.length;i++){
date=date.replace(words[i],"");
}
var d = new Date(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;
t.push([year, month, day].join('-'));
});
console.log(t);
return t;
}
var dates = ['2nd Oct 2014','20th Oct 2014','20th May 2019']
console.log(formatDate(dates));
Upvotes: 1
Reputation: 8740
If you are willing to obtain the result without using any external library, you can try like this using the concept of regular expressions for replacement of st
, nd
, rd
, th
kind of substrings with ''
(blank string) from date strings in array.
I have also used ternary operator
?:
for applying an inline if-else to convert month/date like1
to01
etc.I have also used map() method defined on arrays to skip the use of loop.
function formatDates(dates) {
return dates.map((date) => {
const d = new Date(date.replace(/(st)|(nd)|(rd)|(th)/ig, ""));
const yyyy = d.getFullYear();
const mm = "" + (d.getMonth() + 1);
const dd = "" + d.getDate();
return `${yyyy}-${mm.length === 1? "0" + mm: mm}-${dd.length === 1? "0" + dd: dd}` ;
})
}
var dates = ['2nd Oct 2014', '20th Oct 2014', '20th May 2019']
console.log(formatDates(dates)); // [ '2014-10-02', '2014-10-20', '2019-05-20' ]
Upvotes: 1
Reputation: 2779
Without any external library and nevertheless compact:
function formatDate(d) {
return new Date(d.replace(/^(\d+)\w+/,"$1")).toLocaleString().replace(/^(\d+)\/(\d+)\/(\d+),.*$/, "$3-$2-$1")
}
console.log(['2nd Oct 2014','20th Oct 2014','20th May 2019'].map(formatDate))
Upvotes: 1
Reputation: 14927
If you're using moment, or if using it is an option, it's fairly simple:
const dates=['2nd Oct 2014','20th Oct 2014','20th May 2019'];
dates.forEach(date => {
console.log(moment(date, 'Do MMM YYYY').format('YYYY-MM-DD'));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
Upvotes: 5