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

Correct way to validate time #916

Open
uicowboy opened this issue Oct 6, 2018 · 9 comments · May be fixed by #3729
Open

Correct way to validate time #916

uicowboy opened this issue Oct 6, 2018 · 9 comments · May be fixed by #3729

Comments

@uicowboy
Copy link

uicowboy commented Oct 6, 2018

Hi guys, thanks for the impressive work on this library. I'm looking to replace moment entirely across 4 large projects my team and I are working on. For validating the format of a time string, we do this with moment in some places:

moment('04:30:00', 'HH:mm:ss', true).isValid() // true
moment('04:30:004', 'HH:mm:ss', true).isValid() // false
moment('04:3:00', 'HH:mm:ss', true).isValid() // false

Using date-fns, I gave this a try:

isValid(parse(
  '04:30:00',
  'HH:mm:ss',
  new Date()
)) // true

isValid(parse(
  '04:30:005',
  'HH:mm:ss',
  new Date()
)) // false

isValid(parse(
  '04:3:00',
  'HH:mm:ss',
  new Date()
)) // true   <-- I'd expect this to be false

Maybe I'm not using the right method to achieve this, but I haven't found anything else in the docs and #326 is what inspired me to try parse. What's the correct way to accomplish what I'm looking for here? Thank you!

@kossnocorp
Copy link
Member

kossnocorp commented Oct 11, 2018

isValid does the following:

Returns false if the argument is Invalid Date and true otherwise. The argument is converted to Date using toDate.

At the moment date-fns has no such functionality.

@kossnocorp
Copy link
Member

Sorry, I confused your request with another and closed the issue by mistake.

@kossnocorp kossnocorp reopened this Oct 11, 2018
@kossnocorp kossnocorp added this to the Any release milestone Oct 11, 2018
@TrejGun
Copy link

TrejGun commented Feb 26, 2019

i'm experiencing this issue too

dateFns.parse("02/02/2019 13:1", "dd/MM/yyyy HH:mm")
Sat Feb 02 2019 13:01:00 GMT+0200 (Eastern European Standard Time)

i guess this is wrong

@kossnocorp kossnocorp removed this from the Any release milestone May 11, 2020
@kkomelin
Copy link

kkomelin commented Jun 24, 2020

The same here. The only blocker for complete migration from Moment to date-fns for me is the Moment's strict parsing mode which catches incorrectly formatted date/time.

@kasvith
Copy link

kasvith commented Sep 29, 2021

Im also waiting for this

@KelseyDH
Copy link

I agree, this is badly needed functionality.

@Snowirbix
Copy link

I also need it, is it planned or something ?

@gwagner-intive
Copy link

any news on this???

@yesl-kim
Copy link

yesl-kim commented Mar 7, 2024

@kossnocorp Can I handle this issue??

yesl-kim added a commit to yesl-kim/date-fns that referenced this issue Mar 12, 2024
@yesl-kim yesl-kim linked a pull request Mar 12, 2024 that will close this issue
yesl-kim added a commit to yesl-kim/date-fns that referenced this issue Mar 23, 2024
yesl-kim added a commit to yesl-kim/date-fns that referenced this issue Mar 23, 2024
- add strict digits patterns and utils
- strictly parse the digits according to the options

fixed date-fns#916
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants