Dennis
Dennis

Reputation: 1975

javascript parses date string with browser timezone

var dts = "2019-05-26" // this value came from browser query like "d=1&date=2019-05-26"

var date = new Date(dts)
console.log(JSON.stringify(date))

which prints:

#=> "2019-05-25T19:00:00.0000Z"

Problem

I get this date from user input. Format only contains year, month and day. Problem happens when user browser's timezone applied on parsing. Sometimes, I get correct date in a day but sometimes i get one day before. This causes wrong database querying.

How can I convert this Date object to UTC? Because I need it as a Date object not as a string.

Is there any library that can help me parsing dates at UTC and get back as Date Object?

Upvotes: 0

Views: 41

Answers (1)

Aviad
Aviad

Reputation: 3584

Use Moment UTC to normalize the time

Upvotes: 2

Related Questions