0

I want to convert the digit 250101 into valid date format (YY-DD-MM) in JavaScript.

Assume this **250101 **as 2025/01/01.

I used moment for this.

moment(250101).format(YY-MM-DD). But is giving the invalid date format.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • It’s `moment(String(250101), "YYDDMM")` or, without Moment.js (it’s deprecated): `const dateNumber = 250101, date = new Date(); const { year, month, day } = String(dateNumber).padStart(0, "6").match(/(?\d{2})(?\d{2})(?\d{2})/u)?.groups; date.setFullYear(Number(year) + 2000, month - 1, Number(day)); date.setHours(0, 0, 0, 0); console.log(date);`. It’s unclear whether you need a UTC date or a local date. See [How to initialize a JavaScript Date to a particular time zone](/q/15141762/4642212). – Sebastian Simon Dec 02 '22 at 05:30

0 Answers0