简体   繁体   中英

javascript Date object converts local timezone

i have a model which have date propeties on it. i return this model from server as json object. server send this as json:

model.TaskName = "Task 1";

model.PlannedStartDate = "2015-08-26T15:31:00Z";

but in screen planned start date is 26.08.2015 18:31. when i check the model, javascript date object converts "2015-08-26T15:31:00Z" this date string into a new date object as "Wed Aug 26 2015 18:31:00 GMT+0300 (Turkey Daylight Time)".

i tried in console like :

-> new Date("2015-08-26T15:31:00Z")

<- Wed Aug 26 2015 18:31:00 GMT+0300 (Turkey Daylight Time)

i dont want Date object converts UTC date to local date. how can i do it?

It's not converting it to local time, it's that you're using toString , which outputs the date in local time. If you want the UTC time, use the getUTCXyz methods of Date to create your own string:

 var dt = new Date("2015-08-26T15:31:00Z"); snippet.log("UTC Hour: " + dt.getUTCHours()); snippet.log("UTC Min: " + dt.getUTCMinutes()); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

There's also toUTCString :

 var dt = new Date("2015-08-26T15:31:00Z"); snippet.log("UTC string: " + dt.toUTCString()); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM