I am writing a fairly large webapp in asp.net/c#
with MSSQL 2008 r2
serving the database. The program needs to convert date/time
strings (in ISO date format) to DateTime
where they are used and later stored as smalldatetime
in sql
.
When the strings are converted to datetimes
, an hour
is mysteriously added to the result. I understand that being in the UK, we are subjected to daylight savings time (currently active) but surely the datetime
.convert method understands this? When converting back to a string, the result is as expected.
I have written a small program to illustrate the problem (also including non ISO dates for my sanity):
class Program
{
static void Main(string[] args)
{
//BB();
//Dist();
DateTime d1 = new DateTime();
DateTime d2 = new DateTime();
string d1s = "2010-09-13T09:30:01Z";
string d2s = "2010-09-13 09:30:01";
d1 = Convert.ToDateTime(d1s);
d2 = Convert.ToDateTime(d2s);
Console.WriteLine("d1s:{0} d1:{1} ", d1s, d1);
Console.WriteLine("d2s:{0} d2:{1} ", d2s, d2);
d1s = d1.ToString("u"); d2s = d2.ToString("u");
Console.WriteLine("\nd1: {0}", d1s);
Console.WriteLine("d2: {0}", d2s);
d1 = Convert.ToDateTime(d1s);
d2 = Convert.ToDateTime(d2s);
Console.WriteLine("\nd1s:{0} d1:{1} ", d1s, d1);
Console.WriteLine("d2s:{0} d2:{1} ", d2s, d2);
Console.Read();
}
}
Here are the results I get when I run the program:
d1s:2010-09-13T09:30:01Z d1:13/09/2010 10:30:01
d2s:2010-09-13 09:30:01 d2:13/09/2010 09:30:01
d1: 2010-09-13 10:30:01Z
d2: 2010-09-13 09:30:01Z
d1s:2010-09-13 10:30:01Z d1:13/09/2010 11:30:01
d2s:2010-09-13 09:30:01Z d2:13/09/2010 10:30:01
Done
Is this the correct behavior? Am I being an idiot? I would have expected converting datetime to string and then the exact string back to datetime would give back the original input. If this is the correct behavior, any ideas on how to get a consistant result but still using Convert.ToDateTime()
?
Many Thanks.
Two 'H' in HH is for 24-hour format. Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
to check if the difference is more than 6 hours you can just do abs(start_time - end_time) > timedelta(hours=6) .
Extract time only from datetime with formula 1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range.
With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone. DateTimeOffset reflects a time's offset from UTC, but it does not reflect the actual time zone to which that offset belongs.
The 'Z' at the end of the datetime means "Zulu" (equivalent of UTC/GMT). By default when you convert a string with that at the end of it it will convert it to local time (+ 1 hour in your case).
Without the 'Z' .NET will assume the date is already in the correct format and not add the hour.
When you format the datetime back into a string you are using the format string of "U". This is telling .NET that it is a UTC time and should be formatted at such. Therefore it adds the 'Z' to the end. When you convert it back to a date time another hour is added to make it local.
To clarify:
d1: Starts as a UTC string -> local time (+ 1 hour) -> UTC string -> local time (+1 hour)
d2: Starts as a local string -> local time (no change) -> UTC string -> local time (+ 1 hour)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With