user1096778
user1096778

Reputation: 21

DateTime.Parse error

Our webservice uses the Datetime.parse method to convert data from an xml to DateTime format. It parses Date and time strings separately and adds it together like this -

DateTime.Parse(Date_string).add(TimeSpan.Parse(Time_string)).

Code was working fine except for a few hours last week. Time was showing as 12 hours ahead of actual time. For example, 01/01/2011 10:00:00 will be parsed as 01/01/2011 22:00:00. Most of the requests during that time were processed with datetime values 12 hours ahead of actual time though some were processed correctly. It is working fine now and haven't seen it after that.

Has anyone seen a issue like this?

Upvotes: 2

Views: 523

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241450

You say "Code was working fine except for a few hours last week", but you didn't specify exactly when that was or what time zone you are in. Any chance it was around a daylight savings time change?

You shouldn't use TimeSpan.Parse at all. A TimeSpan does NOT represent the time-of-day, despite its appearance as hh:mm:ss. A TimeSpan represents a fixed DURATION of time.

If you really are given separate date and time strings, join them together before parsing, such as:

DateTime dt = DateTime.Parse(date_string + " " + time_string);

You should also be aware of the timezone implications of the string you are sending in. See the MSDN article on DateTime.Parse for further details.

Upvotes: 1

Related Questions