javascript - Convert date and time to UTC -


i asking user input date , time application. user input time based on timezone in. when save date , time database want convert time utc when query time (which done in utc) can find entries.

this i've done :

var date = new date(); var datestring = "0" + (date.getmonth() + 1) + "/" + date.getdate() + "/" + date.getfullyear() + " " + time; //format date date = moment(datestring, "mm/dd/yyyy hh:mm a"); date = new date(date).toisostring(); 

where time time user enters (ex if want schedule 11:00am, time = 11:00am)

when saved database, looks : isodate("2016-05-09t11:00:00z") not correct since est saved zulu time.

how can convert time (i using moment) saved correct zulu time?

one option use javascript's built-in utc functions.

javascript date reference

getutcdate() - returns day of month, according universal time (from 1-31) getutcday() - returns day of week, according universal time (from 0-6) getutcfullyear()- returns year, according universal time getutchours() - returns hour, according universal time (from 0-23) getutcmilliseconds() - returns milliseconds, according universal time (from 0-999) getutcminutes() - returns minutes, according universal time (from 0-59) getutcmonth() - returns month, according universal time (from 0-11) getutcseconds() - returns seconds, according universal time (from 0-59) 

for example,

new date('2016-05-09 10:00:00')     returns mon may 09 2016 10:00:00 gmt-0400  new date('2016-05-09 10:00:00').getutchours()     returns 14 

update: examples (including .toisostring())

if choose july 4, 2016 @ 8:00 pm eastern time (gmt-0400), utc july 5, 2016 @ 00:00 (midnight):

var date = new date('2016-07-04 20:00:00') date.getutcfullyear = 2016 date.getutcmonth = 6 (0 base) date.getutcdate = 5 date.getutchours = 0 date.getutcminutes = 0  var date = new date('2016-07-04 20:00:00') date.toisostring() = "2016-07-05t00:00:00.000z" (utc) 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -