Home > Blockchain >  Invalid date even if it's exactly the same
Invalid date even if it's exactly the same

Time:04-21

I have following problem.

I have this function

  let time = datapoints.map((datapoint) => {
    let time = dayjs(datapoint.time)

    if (time.$d == 'Invalid Date') {
      console.log(datapoint.time, 'invalid')
    } else {
      console.log(datapoint.time)
    }
    return time
  })

datapoint.time is in this format 12.1.2021, 22:45:00

Now if i console log it, it displays me:

enter image description here

I dont get it, why does it just kind of randomly does not convert this time?

EDIT:

I tried following:

import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
let time = dayjs(datapoint.time, 'DD.MM.YYYY, HH:mm:ss')

But its still invalid date

CodePudding user response:

You are close, change let time = dayjs(datapoint.time, 'DD.MM.YYYY, HH:mm:ss') to let time = dayjs(datapoint.time, 'DD.M.YYYY, HH:mm:ss') as MM expects 2 digit month and you have 1 digit month.

For anyone stumbling across: https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens

  • Related