Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting DateTimeOffset value from SQL 2008 to C#

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?

like image 325
DarLom Avatar asked Apr 19 '10 16:04

DarLom


People also ask

What does DateTimeOffset mean in SQL?

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.

What is DateTimeOffset C#?

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).

How do I get UTC offset in SQL?

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.

What is the difference between DateTime and DateTimeOffset?

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.


1 Answers

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];
like image 155
Andomar Avatar answered Sep 21 '22 11:09

Andomar