-1

I am dealing with an old database. I need to compare dates to each other. One date type is datetime and the other is a string.

When I try to convert

07/25/2019 06:30AM

using new Date(Date.parse(07/25/2019 06:30AM))

I get the error Invalid Date.

benvc
  • 12,401
  • 3
  • 22
  • 45
Darron
  • 151
  • 1
  • 11
  • 1
    read this https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date i think it helps you – Sim1-81 Oct 02 '19 at 13:37
  • The usual suggestion is [MomentJS](https://momentjs.com/). – isherwood Oct 02 '19 at 13:37
  • Possible duplicate of [Date.parse(2/4/2011 9:34:48 AM)](https://stackoverflow.com/questions/5034642/date-parse2-4-2011-93448-am) – benvc Oct 02 '19 at 13:39

4 Answers4

0
new Date('07/25/2019 06:30 AM')

but your date format is invalid because it requires space between time (06:30) and AM

Alex Vovchuk
  • 1,698
  • 3
  • 11
  • 32
0

Try

new Date(Date.parse("07/25/2019 06:30"))

You don't need the AM that is redundant to the fact of the time itself of 06. I can see using that if you were to use 6:30 but not 06:30.

Elijah
  • 2,909
  • 4
  • 28
  • 42
0

You can make it by two ways:

  • Remove AM:
    let dt = new Date('07/25/2019 06:30');
    alert(dt);
  • Add space between hour and AM:
    let dt = new Date('07/25/2019 06:30 AM');
    alert(dt);
Ivan
  • 1
  • 1
0

Try to use the following approach to to avoid some strange bugs while parse Date:

let date_string = "07/25/2019 06:30 AM"
let [M,d,y,h,m,s] = date_string.split(/[/ :]/);
h = (s == 'AM') ? h : (parseInt(h) + 12);    
let yourDate =  new Date(y, parseInt(M)-1, d, h, parseInt(m));
console.log(`yourDate ${yourDate}`);
StepUp
  • 27,357
  • 12
  • 66
  • 120