Converting datetime to different timezones with javascript

◇◆丶佛笑我妖孽 提交于 2020-01-15 11:43:08

问题


Im using the InstantAnswer source of the Bing API to retrieve flight status information in JSON format via jQuery.getJSON. This is an example of what im receiving:

if(typeof SearchCompleted == 'function') SearchCompleted({"SearchResponse":{"Version":"2.2","Query":{"SearchTerms":"LA1442"},"InstantAnswer":{
"Results":[{"ContentType":"FlightStatus","Title":"flight status for Lan Airlines 1442","ClickThroughUrl":"http:\/\/www.bing.com\/search?mkt=en-US&q=LA1442&form=SOAPGN","Url":"http:\/\/www.flightstats.com\/go\/FlightStatus\/flightStatusByFlight.do?id=239598315","InstantAnswerSpecificData":{"FlightStatus":{"AirlineCode":"LA","AirlineName":"Lan Airlines","FlightNumber":"1442","FlightName":"LA1442","FlightHistoryId":239598315,"StatusString":"Landed","StatusCode":76,"OnTimeString":"","ScheduledDeparture":"2011-09-22T17:25:00Z","UpdatedDeparture":"2011-09-22T17:15:00Z","ScheduledArrival":"2011-09-22T18:20:00Z","UpdatedArrival":"2011-09-22T18:17:00Z","OriginAirport":{"Code":"GYE","Name":"Guayaquil","TimeZoneOffset":-18000},"DestinationAirport":{"Code":"UIO","Name":"Quito","TimeZoneOffset":-18000},"DepartureGate":"","DepartureTerminal":"","ArrivalGate":"","ArrivalTerminal":"","PreviousSegment":{"FlightHistoryId":239598314,"OriginAirport":"EZE","DestinationAirport":"GYE"},"DataFreshness":1}}}]}}} /* 

The information im having trouble with (timezone related) is this:

  • Scheduled Departure: 2011-09-22T17:25:00Z
  • Scheduled Arrival: 2011-09-22T18:20:00Z

Here is the problem: If the flight has already departed, i need to make a projection of which percentage of the flight path has been already travelled.

That would be easy to calculate IF both the Scheduled Departure and Scheduled Arrival fields would be in the same timezone as the computers current timezone (im using a clientside script only) -- but they are not. One of them is using the timezone in Ecuador (GMT -5) and the other is using the timezone in Chile (GMT -4).

My idea is:

  1. convert both values to unix timestamps
  2. convert both timestamps to the current computer's timezone
  3. calculate plane's travelled percentage with those 2 timestamps and my computer's current timestamp

The step im having trouble is No. 2 - Any ideas? Thanks!

来源:https://stackoverflow.com/questions/7520478/converting-datetime-to-different-timezones-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!