0

I'm manipulating some data coming from an external XML that I cannot change. This XML has dates in this format:

01-APR-39

and I want the date to become in this format:

01/04/1939

but using:

new Date('01-APR-39').toLocaleDateString('en-GB')

is returning wrong century:

01/04/2039

how can I fix this?

silvered.dragon
  • 407
  • 1
  • 7
  • 19
  • How is the parser supposed to know which century you want? See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 19 '20 at 08:27

1 Answers1

0

Like this.

You need to decide the year cutoff

const cutOff = 29; // or have a range
const getDateFromYY = dStr => {
  let [dd, mmm, yy] = dStr.split("-");
  const yyyy = +yy > cutOff ? "19" + yy : "20" + yy;
  return new Date(dd + "-" + mmm + "-" + yyyy).toLocaleDateString('en-GB')
};

console.log(getDateFromYY('01-APR-39'))

console.log(getDateFromYY('01-APR-19'))
mplungjan
  • 169,008
  • 28
  • 173
  • 236