Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse timezone with datetime to a datetime parameter

I am using saber soap API for flight Booking.

I have an DateTime object which requires a parameter with the timezone. The input should be like "2016-03-01T10:00:00-06:00"

At the moment I am getting the value like

DateTime dt = DateTime.UtcNow;
string date = dt.ToString();
string tstamp = dt.ToString("mm-dd-yyyyTHH:mm:sszzz");
DateTimeOffset tstamp = DateTimeOffset.Parse(date);
DateTime datetime = tstamp.DateTime;

but when I convert it back to DateTime the timezone gets removed automatically. I cannot convert the object to DateTimeoffset because the API requires it in the DateTime format.

like image 755
gayangi Amarawardena Avatar asked Jun 23 '16 10:06

gayangi Amarawardena


1 Answers

I think this could help you, it is possible to get the dateTime you desire or to get the offset as TimeSpan and then add it to your datetime if needed or save it.

Edit: now I see you maybe just need to get the string from a DateTimeOffset object, not from a DateTime object.

DateTime dt = DateTime.UtcNow;
string date = dt.ToString();
string tstampString = dt.ToString("MM-dd-yyyyTHH:mm:ssZZZ");

DateTimeOffset tstampDT = DateTimeOffset.Parse(date);

DateTime datetimeCurrent = tstampDT.DateTime;
DateTime datetimeUTC = tstampDT.UtcDateTime;
DateTime datetimeLocal = tstampDT.LocalDateTime;
TimeSpan offsetFromUTC = tstampDT.Offset;

edit:

string tstampOffsetString = tstampDT.ToString("MM-dd-yyyyTHH:mm:sszzz");
like image 179
Tommaso Cerutti Avatar answered Oct 10 '22 08:10

Tommaso Cerutti