0

Is there a way to reformat a date using .replace and a regex? or a series of them?

for example I would like to turn May 4, 1981 into 1981-May-04... this would be good enough for my needs.

but even better would be to turn May 4, 1981 into 1981-05-04.

please note that single digit months and dates need to be changed to double digit (i.e. prefix a 0). The source text May 4, 1981 would not contain a leading 0 for the day ever.

the end result YYYY-MM-DD being sortable, which is why the leading 0 is important.

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • 1
    Does this answer your question? [How do I format a date in JavaScript?](https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript) – pilchard May 07 '22 at 14:37
  • also: [Easiest way to convert month name to month number in JS ? (Jan = 01)](https://stackoverflow.com/questions/13566552/easiest-way-to-convert-month-name-to-month-number-in-js-jan-01) – pilchard May 07 '22 at 14:39

1 Answers1

0

new Date('May 4, 1981').toLocaleDateString('en-CA') outputs '1981-05-04'

toLocaleDateString is pretty powerful - check it out: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Alternatives:

  1. Write your own simple function to extract the date data, irrespective of the locale.

const date = new Date('May 4, 1981');

function getMyFormat(date) {
  return [
      date.getFullYear(), 
      date.getMonth().toString().padStart(2, '0'),
      date.getDay().toString().padStart(2, '0')
  ].join('-');
}

console.log(getMyFormat(date))
  1. Use the ISO string (standardized format of time representation here)
new Date(date).toISOString().split('T')[0]

It spits out a long string where, before the 'T', the format is YYYY-MM-DD.

There are some caveats to this, however, as it puts the date in the UTC timezone, which could affect your setup.

There is, however, an easy way to handle this:

new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000).toISOString().split('T')[0]

Over here, we calculate the timezone offset with respect to UTC so it should help you get the format you need.

Azarro
  • 1,776
  • 1
  • 4
  • 11
  • Just flag as [duplicate](https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript) (or at the very least point out the possible pitfalls of `Date.parse()` see: [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results)) – pilchard May 07 '22 at 14:42
  • for some weird reason that gives me `Monday, May 04, 1981` – ycomp May 07 '22 at 16:00
  • @ycomp You tried it with the "en-CA" parameter? That, along with a few other locales, use that format (https://stackoverflow.com/a/60012660/16323922). – Azarro May 07 '22 at 21:03
  • 1
    @ycomp I added a couple of alternate solutions – Azarro May 07 '22 at 21:12
  • @Azarroas a sure I tried en-CA and it gave me the same result. I ended up going with the ISO and offset and that works for me. – ycomp May 09 '22 at 15:30