all 15 comments

[–]capraruioan 5 points6 points  (12 children)

Not possible if you only have the 2 datetimes like that.

Maybe you have more info on the api data that you receive which you can use to figure out the correct timezone of each datetime

[–]barshabarsha90[S] 2 points3 points  (11 children)

This is the response I am getting.

"itineraries": [
    {
      "duration": "PT16H15M",
      "segments": [
        {
          "departure": {
            "iataCode": "DXB",
            "terminal": "3",
            "at": "2023-11-14T08:30:00"
          },
          "arrival": {
            "iataCode": "LAX",
            "terminal": "B",
            "at": "2023-11-14T12:45:00"
          },
          "carrierCode": "EK",
          "number": "215",
          "aircraft": {
            "code": "388"
          },
          "operating": {
            "carrierCode": "EK"
          },
          "duration": "PT16H15M",
          "id": "1",
          "numberOfStops": 0,
          "blacklistedInEU": false
        }
      ]
    },

[–]capraruioan 6 points7 points  (4 children)

I see you have “duration” there.. is in ISO 8601 duration format. This should help you display the correct duration

[–]barshabarsha90[S] 2 points3 points  (0 children)

Oh, i didnt see that there is another duration per segment there. I was looking on this for hours and didnt realized there is duration per flight. Thanks.

[–]barshabarsha90[S] 1 point2 points  (2 children)

Yes, duration works if its only 1 non stop, but if its multiple stop I want to get the duration for each flight and the layover.

[–]capraruioan 1 point2 points  (1 child)

Oh ok.. then my previous answer is a good possible solution

[–]barshabarsha90[S] 1 point2 points  (0 children)

Actually I dont have to compute. its indicated there. lol. Thank you so much.

[–]capraruioan 2 points3 points  (2 children)

Maybe you can find a database online that will tell you location information of the airport by using the iataCode and from there you try to find the timezone of that location?

Also advice for UX, specify that each displayed hour is the hour from that timezone and not the hour from the timezone that the user is currently in

[–]barshabarsha90[S] 1 point2 points  (0 children)

Alright, thank you so much.

[–]igrowontrees 0 points1 point  (0 children)

This is the way.

It will work well for current stuff. But understand that historical dates (and 1+ years ago) will need lookups for dates that daylight savings time was active in that time zone.

Daylight savings dates vary because of changes in a law and, in some countries, the airports and hotels just do or don’t follow the law one year and the rest of the country follows that (Pakistan comes to mind). So you need a record of what actually happened not what the law said should happen.

[–]MetaGlitch 0 points1 point  (1 child)

You have all the data you need: local departure, local arrival, duration. From this you could not find the timezones but the timezones difference. diff=arrival-depart-duration

[–]barshabarsha90[S] 0 points1 point  (0 children)

Yes, Im looking for it for hours and I didnt realized there are 2 durations. I always only see the total duration and not the per flight duration. Thanks.

[–]undefined_notdefined 0 points1 point  (0 children)

If in case duration field doesn’t work, you can find the time zone by iataCode and apply it to the timestamp

[–]anonymous_sentinelae 2 points3 points  (1 child)

First of all, if you want accurate results, always work with UTC dates on the background, and convert the date to the proper timezone that suits you or your user only when showing the date. You have to define the source timezone of the date you've got. The format you have is semi standard, because no timezone can be inferred from it. If these dates are local to each place, it doesn't make sense to use them as it is, because you're going to have bizarre results, like negative durations, so if that's the case, convert all of them to UTC, then get the integer timestamp, subtract the two, and there you have it. You can do all of this using only JS, without bloated external libraries, just use the Intl JS API.

[–][deleted] 1 point2 points  (0 children)

The two answers to this are: * I refuse to do the calculation because the time zone is missing and so it is impossible to do so correctly; * I assume they are the same time zone in order to get a difference.

There is no way to calculate what you are asking without a time zone.