13

I have the following code


import dayjs from 'dayjs'

const abc = dayjs("09:00:00")
console.log(abc)

abc in console is

an invalid date

how can I make this into a valid date, the condition being the input is always going to be in format "09:00:00"

Vikrant Bhat
  • 2,117
  • 2
  • 14
  • 32

2 Answers2

24

To get this to work, you'll need to enable the CustomParseFormat plugin. Then you can specify a format string for dayjs to use. For example:

const abc = dayjs("09:00:00", "HH:mm:ss");
console.log(abc);

Will result in the following:

The result of abc

You can read about the different options for the format string at the dayjs documentation: https://day.js.org/docs/en/parse/string-format

Alex Studer
  • 592
  • 3
  • 13
13

If you installed dayjs using npm, you can use CustomParseFormat plugin like this.

import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";

dayjs.extend(customParseFormat);
const day = dayjs("09:00:00", "HH:mm:ss");
console.log(day);
Capella
  • 881
  • 3
  • 19
  • 32