RestSharp changes the timezone on my datetime-objects during serialization. The original datetime object is for example 2015-01-02 00:00:00
when I debug, but when RestSharp serializes it and I add it to my request using request.AddBody(object)
, the payload has turned the date to 2015-01-01T22:00:00Z
. Therefore it has converted it back in time with 2 hours.
I'm currently in Sweden where it's GMT+2:00 so I'm guessing it's going to default GMT+0?
Update
I get a date in form of a String
from the client, for example "2015-01-02"
.
Then I try to parse it to a DateTime
-object in the following code:
if (!string.IsNullOrEmpty(fieldData) && DateTime.TryParse(fieldData, out dateTime)){
budgetUtokad.SlutDatum = dateTime;
}
I need to specify the Utc
in some kind of way here?
Well if you're happy getting "midnight at the start of the given date, in UTC" you can use:
if (!string.IsNullOrEmpty(fieldData) &&
DateTime.TryParseExact(
fieldData,
"yyyy-MM-dd",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
out dateTime)) {
budgetUtokad.SlutDatum = dateTime;
}
That's then in UTC already, so will be output by RestSharp as "2015-01-02T00:00:00Z".
See more on this question at Stackoverflow