I have a SQL 2008 table with a field called RecDate of type DateTimeOffset
.
For a given record the value is '2010-04-01 17:19:23.62 -05:00'
In C# I create a DataTable and fill it with the results of
SELECT RecDate FROM MyTable.
I need to get the milliseconds, but if I do the following the milliseconds are always 0:
DateTimeOffset dto = DateTimeOffset.Parse(dt.Rows[0][0].ToString());
What is the proper way to get the value in the RecDate column into the DTO variable?
The DATETIMEOFFSET data type is a date and time with time zone awareness. DATETIMEOFFSET supports dates from 0001-01-01 through 9999-12-31. The default value is 1900-01-01 00:00:00 00:00. The time is based on 24-hour UTC clock. UTC = Universal Time Coordinate or Greenwich Mean Time.
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).
Extracting the Time Zone Offset You can use the DATEPART() function to return the time zone offset. This function returns an integer that represents the time zone offset in minutes. You can also use the FORMAT() function to return the time zone offset as a string.
DateTimeOffset contains information about the timezone you are dealing with, which makes all the difference in THE WORLD! The only difference is that it stores only the UTC offset for the particular instant in time that a DateTime represents.
Perhaps the cast to ToString()
removes the microsecond info.
According to MSDN, the SQL Server data type datetimeoffset
matches C#'s DateTimeOffset
. So it should be safe to cast a datetimeoffset
column to DateTimeOffset
.
For example:
DateTimeOffset dto = (DateTimeOffset) Rows[0][0];
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