Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Date parsing a time without a date #1552

Open
lsorin77 opened this issue Jun 25, 2021 · 7 comments
Open

Invalid Date parsing a time without a date #1552

lsorin77 opened this issue Jun 25, 2021 · 7 comments

Comments

@lsorin77
Copy link

lsorin77 commented Jun 25, 2021

Something like dayjs('4:43', 'h:mm') is returning an Invalid Date. If you add in a date, e.g. dayjs('12-25-1995 4:43', 'MM-DD-YYYY h:mm'), then it works. I need to be able to parse just the time, without a date, which is what I am currently doing with momentjs. I am using Typescript/Angular, dayjs version 1.10.5. extending customParseFormat plugin, which is probably inferred from it working with the date.

@imwh0im
Copy link
Contributor

imwh0im commented Jun 29, 2021

Tried this issue.

  const input = '4:30';
  const format = 'h:mm';

  const result = dayjs(input, format);
  const result_1 = moment(input, format);

  console.log(result.format('YYYY-MM-DD HH:mm:ss'));  // 2021-06-29 04:30:00
  console.log(result_1.format('YYYY-MM-DD HH:mm:ss'));  // 2021-06-29 04:30:00

But in my case it was same result in "momentjs" and "dayjs".
Can you explain the issue in more detail?

@lsorin77
Copy link
Author

lsorin77 commented Jun 29, 2021

console.log("dayjs 4:43", dayjs('4:43', 'h:mm').toDate());
returns an Invalid Date

console.log("moment 4:43", moment('4:43', 'h:mm').toDate());
returns "2021-06-29T09:43:00.000Z"

Another example/use case for parsing just the time portion to validate the time input is valid:
const parsed = dayjs(
value,
['h:mm A', 'h:mm:ss A', 'h:mm:ss.SSSSSSSSS A', 'HH:mm:ss.SSSSSSS', 'HH:mm:ss', 'HH:mm'],
true,
);
console.log(parsed.isValid()); // false

    const parsed = moment(
        value,
        ['h:mm A', 'h:mm:ss A', 'h:mm:ss.SSSSSSSSS A', 'HH:mm:ss.SSSSSSS', 'HH:mm:ss', 'HH:mm'],
        true,
    );
    console.log(parsed.isValid()); // true

@imwh0im
Copy link
Contributor

imwh0im commented Jun 30, 2021

In my case.

import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';

dayjs.extend(customParseFormat);  // try this.

console.log(dayjs('4:43', 'h:mm').toDate());  // 2021-06-29T19:43:00.000Z

Add plugin customParseFormat and extend it.

@lsorin77
Copy link
Author

That is exactly what I'm doing, except my import is
import customParseFormat from 'dayjs';
Angular version 11

@imwh0im
Copy link
Contributor

imwh0im commented Jun 30, 2021

Not

import customParseFormat from "dayjs";

Should be

import customParseFormat from "dayjs/plugin/customParseFormat";

Try it.
If has some problems or already tried it. uninstall "dayjs" and reinstall that.
I hope this will helps

@lsorin77
Copy link
Author

Thanks, that fixed the time parsing, but now I am having an issue with parsing the following datetime string:
dayjs('2021-06-30T16:55+00:00', 'YYYY-MM-DD[T]HH:mmZ', true)
This returns an Invalid Date but works in moment
It appears to work if I remove the strict=true parameter.
I found the following open issue -- #929. Is this related to that?

@imwh0im
Copy link
Contributor

imwh0im commented Jul 6, 2021

i guess same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants