Search code examples
javascriptecmascript-temporal

Date object to Temporal PlainYearMonth


i have a javascript date object as input data, and i want to have a temporal PlainYearMonth output.

i tried this,

const inputDate = new Date();
    const outputData = Temporal.PlainYearMonth.from(inputDate.toTemporalInstant().toString({smallestUnit: 'minute'}));
    console.log(outputData);

but i get this error,

Uncaught RangeError: Z designator not supported for PlainYearMonth

Solution

  • i found a alternative way to get the PlainYearMonth from a instant.

     const inputDate = new Date();
        const outputData = inputDate.toTemporalInstant()
            .toZonedDateTimeISO(timeZone).toPlainYearMonth();
        console.log(outputData);