1

I have one String-formatted Datetime.

I want to give an error if a value has been selected in the 12-hour interval after the current date. However, I do not know how to change data.

I used the following line of code.

// this.startDate0 = Input value (string) (Value: 31/07/2020 20:00)
console.log(Date.parse(this.startDate0)); // Maybe NaN maybe 1581099600000

I did not understand how to do it, can you help?

Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
  • why is the first line commented out? – Rick Jul 30 '20 at 18:24
  • Please show example of the startDate value? – Noah Stahl Jul 30 '20 at 18:26
  • Could you please give the string you used. It probably is giving you milliseconds. – Saiansh Singh Jul 30 '20 at 18:29
  • Coming through Vue Data, I have made you a comment line for information purposes. It normally comes as data. –  Jul 30 '20 at 18:41
  • @NoahStahl Now edited code line. (Value: 31/07/2020 20:00) Example.. –  Jul 30 '20 at 18:43
  • The problem is you are providing javascript with a already formated date / time. Provide it in ISO format and format it later, it will fit best. – Marco Jul 30 '20 at 18:48
  • I recommend you to use moment js, which has more options for formats and conversions. Check this out https://stackoverflow.com/questions/22184747/parse-string-to-date-with-moment-js#answer-44134515. moment js can be added through npm install moment – Sowmyadhar Gourishetty Jul 30 '20 at 19:36

1 Answers1

1

If you're open to using a library, I'd suggest using the date-fns parse function. Supply the expected pattern as a parameter and it will parse the value. You can then use the helper differenceInHours method to compare with 12 hours from present.

Noah Stahl
  • 6,905
  • 5
  • 25
  • 36