1

I am trying to convert my array from this format

ka_layers2 = ["1-05-2019","2-05-2019" ........,"15-05-2019",.."27-05-2019"]

to something like this,

"01-05-2019","02-05-2019"

And here is my code to do so

for(i = 0; i < ka_layers2.length; i++){
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  var d = new Date(ka_layers2[i]);
  var day = console.log("0"+d.getDate()).slice(-2);
  console.log(day);
  ka_layers2arr.push(day + '-' + months[d.getUTCMonth()] + '-' + d.getUTCFullYear());
}

But this giving me a problem with "Cannot read property 'slice' of undefined"

I'm trying with answer mentioned in this question. Javascript add leading zeroes to date

Musthafa
  • 542
  • 1
  • 9
  • 25
  • Why are you calling the `slice` method on `console.log` instead of an instance of an array? – Edric Sep 21 '19 at 07:11

5 Answers5

2

Using regex

/\b(\d)\b/g demo

let dates = ["1-05-2019", "2-05-2019", "15-05-2019", "27-05-2019"]

let result = dates.map(date => date.replace(/\b(\d)\b/g, '0$1'))

console.log(result)
User863
  • 19,346
  • 2
  • 17
  • 41
1

You could split the string, pad the parts with the wanted zeros and join the parts back to a single string.

var array = ["1-05-2019", "2-05-2019", "15-5-200", "27-05-2019"],
    result = array.map(s => s.split('-').map((v, i) => v.padStart([2, 2, 4][i], '0')).join('-'));
    
console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You can split on - and check if the length of first part is less than 2 than add a leading zero else keep it as it is, and than join with -

let arr = ["1-05-2019", "2-05-2019", "15-05-2019", "27-05-2019"]

let getYearsWithZero = (str) => {
  let [date, ...rest] = str.split('-')
  date = date.length < 2 ? '0' + date : date
  return [date, ...rest].join('-')
}

arr.forEach(v => console.log(getYearsWithZero(v)))

One more way is to use replace

let arr = ["1-05-2019", "2-05-2019", "15-05-2019", "27-05-2019"]

let getYearsWithZero = (str) => {
  return str.replace(/^([^-]+)/, (m, g1) => g1.length < 2 ? "0" + g1 : g1)
}

arr.forEach(v => console.log(getYearsWithZero(v)))

Note:- The above method just changes the date, if you need the same functionality for all the parts of date you can extend accordingly

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1
ka_layers2 = ["1-05-2019","2-05-2019", "15-05-2019", "27-05-2019"];

for(i = 0; i < ka_layers2.length; i++){
  if (ka_layers2[i].indexOf("-") === 1) {
    ka_layers2[i] = "0" + ka_layers2[i];
  }
}

console.log(ka_layers2);
qristjan
  • 183
  • 5
0

Thanks to all the answers. I also tried something like this and it worked.

for(i = 0; i < ka_layers2.length; i++){
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var d = new Date(ka_layers2[i]);
        var day = "0"+d.getDate()
        var new_day =  day.slice(-2);
        ka_layers2arr.push(new_day + '-' + months[d.getUTCMonth()] + '-' + d.getUTCFullYear());
       }
Musthafa
  • 542
  • 1
  • 9
  • 25