-3

I'm trying to do something with flight times.

If I'm travelling on July 5th at 23:45 EST and I land at 01:30 EST I know I can build the DateTime for my departure time; but how would I go about making a DateTime for the arrival time?

I would like it to say July 6th 01:30. I want to do this in c# but don't know how to go about doing this.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
Sholan
  • 7
  • 2
  • 1
    do you try using `AddHour()` and `AddMinutes()` methods? – Hossein Badrnezhad Jul 14 '18 at 04:24
  • What are your inputs other than the departure time? For example, if you know the departure time and the duration of the flight, you can use [`DateTime.Add()`](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.add) method. – 41686d6564 stands w. Palestine Jul 14 '18 at 04:25
  • [Creating a DateTime in a specific Time Zone](https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp?answertab=active#tab-top). -- [Daylight saving time and time zone best practices](https://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices?answertab=active#tab-top). – Jimi Jul 14 '18 at 04:30

3 Answers3

0

Your question is a little vague. Im unsure as to what you want to do. Do you want to calculate the date of arrival based upon the date of departure and flight time or do you have the time of departure and arrival. In any case the DateTime object has an AddDays method.

DateTime.AddDays(addedtime As Double)

Note you can add fractional days thus for example if you knew your arrival time would be 1:30 am the next day you would set another variable to equal the departure DateTime and then use the AddDays method as follows

RefrenceToArrivalDateTime.AddDays(1.0625)

Hope this helped. Please ensure that you check the stack community before you ask a question as I am positive this must have been answered before and that you clearly phrase your questions

Happy coding !

0

You can define the duration of the flight as a TimeSpan:

var flightDuration = new TimeSpan(1, 45, 0);

I'd suggest using DateTimeOffset rather than DateTime, since it includes the time zone as part of the value.

If you use the + operator to add a TimeSpan duration to a starting DateTimeOffset, you get the result as a DateTimeOffset. (This also applies to DateTimes, if you prefer to stick with those.)

Use ToString() to format it, like you would with DateTime, passing it a string like "MMMM d HH:mm"; but be aware that there is currently no way of formatting ordinals, so although it's easy to get "July 6 01:30", there's no easy way to get "July 6th 01:30".

More information about Choosing between DateTime and DateTimeOffset.

Richardissimo
  • 5,596
  • 2
  • 18
  • 36
0

Try:

DateTime departure = new DateTime(2018, 7, 5, 23, 45, 0); 
DateTime arrival = departure + new TimeSpan(1, 45, 0);
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69