This article highlights the main differences between the datetime and datetimeoffset data types in SQL Server. Both data types are used for storing date and time values. But there are significant differences between the two. Perhaps the most obvious difference is that the datetimeoffset stores the time zone offset, whereas datetime doesn’t.
Full Answer
How to compare datetime with only date in SQL Server?
The parameters used in the above syntax are as follows :
- column_name1, column_name2, …: Field or columns which we want to fetch for the final result set.
- table_name1: The database table from which the said columns will be fetched from.
- column_name1: Date value either in date data type or string format that can be converted to date using :: DATE (data type conversion function). ...
How can I truncate a datetime in SQL Server?
There are some ways to do this:
- casting;
- manipulating with string conversion;
- working on total number of years/months/days/hours in DateTime value.
How to add minutes to datetime in SQL Server?
Parameter Values
- year, yyyy, yy = Year
- quarter, qq, q = Quarter
- month, mm, m = month
- dayofyear, dy, y = Day of the year
- day, dd, d = Day
- week, ww, wk = Week
- weekday, dw, w = Weekday
- hour, hh = hour
- minute, mi, n = Minute
- second, ss, s = Second
How to get SQL Server date in its native format?
- SYSDATETIME (): To returns the server’s date and time
- SYSDATETIMEOffset (): It returns the server’s date and time, along with UTC offset
- GETUTCDATE (): It returns date and GMT (Greenwich Mean Time ) time
- GETDATE (): It returns server date and time
Is DateTimeOffset better than DateTime?
Converting between DateTime and DateTimeOffset The DateTimeOffset structure provides more time zone awareness than the DateTime structure.
What is the difference between DateTimeOffset and DateTime in C#?
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.
What is DateTimeOffset?
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).
Should I use DateTimeOffset?
DateTime values lack any knowledge of time zone, or lack thereof. If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.
Is DateTimeOffset UTC?
Remarks. The UtcDateTime property performs a dual conversion: It converts the date and time of the current DateTimeOffset object to Coordinated Universal Time (UTC). The conversion is performed by subtracting the value of the Offset property from the date and time of the current DateTimeOffset object.
What is DateTimeOffset in SQL?
The SQL Server DateTimeOffset data type stores the date & time along with the Time Zone Offset. It is similar to both DateTime & DateTime2 data types. Except that the DateTime & DateTime2 does not store the Time Zone Offset. Also DateTime is less precise than DateTime2.
How do I convert DateTimeOffset to local time?
In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. It then converts the UTC date and time to local time by adding the local time zone offset.
What is the difference between DateTime and Datetime2 in SQL Server?
Datetime2 was introduced with SQL Server 2008, so it is here long enough to draw some comparisons with its “older brother”. The main difference is the way of data storage: while in Datetime type, the date comes first and then time, in Datetime2, 3 bytes, in the end, represents date part!
What is EDM DateTimeOffset?
Edm. DateTimeOffset - represents the date and time as an offset in minutes from GMT, with values from 12:00:00 midnight, January 1, 1753 A.D. through to 11:59:59 P.M, December 9999 A.D. For example, 1999-01-01T23:01:00Z corresponds to 11:01:00 PM on January 1, 1999.
Should I use now or UtcNow?
UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime. Now gives the date and time as it would appear to someone in your current locale.
What is the difference between DateTime now and DateTime today?
The DateTime. Now property returns the current date and time, for example 2011-07-01 10:09.45310 . The DateTime. Today property returns the current date with the time compnents set to zero, for example 2011-07-01 00:00.00000 .
What is difference between DateTime now and DateTime UtcNow?
The property Now of the DateTime class returns the current date and time of the machine running the code, expressed in the computer's local time. The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format.
What is datetimeoffset structure?
The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time.
What is a datetime?
It includes a Kind property that provides limited information about the time zone to which that date and time belongs. The DateTimeKind value returned by the Kind property indicates whether the DateTime value represents the local time ( DateTimeKind.Local ), Coordinated Universal Time (UTC) ( DateTimeKind.Utc ), or an unspecified time ( DateTimeKind.Unspecified ).
How to use date and time in a.NET application?
.NET applications can use date and time information in several ways. The more common uses of date and time information include: 1 To reflect a date only, so that time information is not important. 2 To reflect a time only, so that date information is not important. 3 To reflect an abstract date and time that is not tied to a specific time and place (for example, most stores in an international chain open on weekdays at 9:00 A.M.). 4 To retrieve date and time information from sources outside of .NET, typically where date and time information is stored in a simple data type. 5 To uniquely and unambiguously identify a single point in time. Some applications require that a date and time be unambiguous only on the host system. Other apps require that it be unambiguous across systems (that is, a date serialized on one system can be meaningfully deserialized and used on another system anywhere in the world). 6 To preserve multiple related times (such as the requestor's local time and the server's time of receipt for a Web request). 7 To perform date and time arithmetic, possibly with a result that uniquely and unambiguously identifies a single point in time.
What is timezoneinfo?
The TimeZoneInfo class represents any of the Earth's time zones, and enables the conversion of any date and time in one time zone to its equivalent in another time zone . The TimeZoneInfo class makes it possible to work with dates and times so that any date and time value unambiguously identifies a single point in time. The TimeZoneInfo class is also extensible. Although it depends on time zone information provided for Windows systems and defined in the registry, it supports the creation of custom time zones. It also supports the serialization and deserialization of time zone information.
How to retrieve date and time?
To retrieve date and time information from sources outside of .NET, typically where date and time information is stored in a simple data type. To uniquely and unambiguously identify a single point in time. Some applications require that a date and time be unambiguous only on the host system. Other apps require that it be unambiguous ...
How to take advantage of time zone support in NET?
To take advantage of time zone support in .NET, you must know the time zone to which a date and time value belongs when that date and time object is instantiated. The time zone is often not known, particularly in web or network apps.
What is the purpose of date and time?
The more common uses of date and time information include: To reflect a date only, so that time information is not important. To reflect a time only, so that date information is not important. To reflect an abstract date and time that is not tied to a specific time and place (for example, most stores in an international chain open on weekdays ...
What does UTC mean in time?
It assumes that a date and time value whose offset is TimeSpan.Zero represents UTC. In fact, UTC is not a time in a particular time zone, but the time in relation to which the times in the world's time zones are standardized. Time zones can also have an offset of Zero.
Is datetimeoffset more important than datetime?
Although the DateTimeOffset structure provides a greater degree of time zone awareness than the DateTime structure, DateTime parameters are used more commonly in method calls. Because of this, the ability to convert DateTimeOffset values to DateTime values and vice versa is particularly important. This topic shows how to perform these conversions in a way that preserves as much time zone information as possible.
Does datetime reflect UTC?
Both the DateTime and the DateTimeOffset types have some limitations when representing times in time zones. 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. For details about time values and support for time zones, see Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo.
What is the difference between datetimeoffset and datetimeoffset?
Another important difference is that datetimeoffset allows you to specify the precision (up to 7 decimal places). This means that datetimeoffset values can vary in their storage size, depending on the precision being used. The datetime type on the other hand, has a fixed storage size and precision.
Why is there an extra byte in datetimeoffset?
An extra byte is added to the datetimeoffset value but not to the datetime value. This is because the datetimeoffset value needs an extra byte to store the precision (because the precision is user-defined). The datetime value on the other hand has a fixed precision, so there’s no need for the precision to be stored with the value. ...
How many bytes are in datetime?
The datetime data type uses 8 bytes. The datetimeoffset data type uses either 8, 9, or 10 bytes, depending on its precision. Therefore, you’re not saving any storage size by using datetime. However, if you convert a datetimeoffset value to a binary constant, it does add 1 byte in order to store precision.
Does SQL Server have a datetimeoffset?
While it’s true that SQL Server stores its date and time values in hexadecimal, that hex value doesn’t actually include the precision when storing datetimeoffset values. This is because the precision is included in the column definition. For more detail on how this data type is stored in the database, see Understanding ‘datetimeoffset’ Storage Size ...
Do you need to use datetimeoffset?
If you need to include a time zone offset, then you’ll need to use datetimeoffset. If not, then datetime may suffice. However, Microsoft recommends that you use datetime2 for new work, as it has many benefits over datetime. See datetime vs datetime2 for a comparison on these data types.
Does SQL Server store datetime?
The datetime value on the other hand has a fixed precision, so there’s no need for the precision to be stored with the value. Many developers assume that converting to varbinary is representative of how SQL Server actually stores date and time values. However this is only partially true. While it’s true that SQL Server stores its date ...
How many bytes is datetime2?
The datetime2 can be either 6, 7, or 8 bytes, depending on its precision. The datetimeoffset can be either 8, 9, or 10 bytes, depending on its precision. Microsoft states that the datetime2 type also uses 1 extra byte in order to store its precision, in which case it would use at least 3 bytes more than smalldatetime.
Do you need to include a time zone offset?
This depends on whether or not you need to include a time zone offset. If you need to include a time zone offset, then you’ll need to use datetimeoffset. If not, then use datetime2, as you’ll save storage space and eliminate any potential issues with having a (potentially wrong) time zone offset in your data.
Can you use datetime2 to lower precision?
Both types allow you to specify a precision (by using a scale between 0 and 7). Therefore, it’s possible to set the datetime2 value to a lower precision than the datetimeoffset value (and vice versa).
Storing DateTime Values
The simple DateTime type ( which makes a great example of a Value Object ), always assumes the local machine's relative time. When you ask it for .Today or .Now it uses the local system clock. This can easily cause problems between machines with different times and/or timezones.
DateTimeOffset
DateTimeOffset is both a .NET type and a SQL Server type (other databases have equivalents, too). The main difference between it and the simpler DateTime type we all know and love is that it includes a time zone offset from UTC. Thus, it's always clear when looking at a DateTimeOffset what time is meant, whether UTC or local.
DateTimeOffset in SQL Server
SQL Server supports both DateTime and DateTimeOffset values. DateTimeOffset uses variable precision and so can take up more space than DateTime, but doesn't always do so. Compare DateTimeOffset and DateTime on SQL Server and choose which one makes sense for your needs. If you just want to quickly see the difference, run these queries:
Aside
This idea to write this article comes from a bug in the current DevBetter.com website source code caused by using a DateTime property rather than a DateTimeOffset.
Summary
DateTime values lack any knowledge of time zone, or lack thereof. If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.
Also on Ardalis
When designing web APIs, it's important to think about how the data being passed to and from the endpoint will be structured. How important is reuse in these considerations, and how much reuse can you get away with before it starts causing other problems?
Question
I have used DateTime structure uptill now. But, now I am considering moving to DateTimeOffset as I am working across Timezones. I read the entire MSDN article: " Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo"
All replies
What is the use of date/time? It establishes a point in time, used either for reference (recording the time of an event), or for display (what time is it--was/is/will be). For me the question is simple: If I am persisting the time of an event (past or future), I use UTC. That way the event is recorded without regard to timezone.

The DateTimeOffset Structure
- The DateTimeOffsetstructure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time. The DateTimeOffset type includes all of the functionality of the DateTimetype along with time zone awareness. This makes it suitable for ap...
The Datetime Structure
- A DateTime value defines a particular date and time. It includes a Kind property that provides limited information about the time zone to which that date and time belongs. The DateTimeKind value returned by the Kind property indicates whether the DateTime value represents the local time (DateTimeKind.Local), Coordinated Universal Time (UTC) (DateTimeKind.Utc), or an unspec…
The Timespan Structure
- The TimeSpanstructure represents a time interval. Its two typical uses are: 1. Reflecting the time interval between two date and time values. For example, subtracting one DateTime value from another returns a TimeSpanvalue. 2. Measuring elapsed time. For example, the Stopwatch.Elapsed property returns a TimeSpan value that reflects the time interval that has ela…
The TimeZoneInfo Class
- The TimeZoneInfo class represents any of the Earth's time zones, and enables the conversion of any date and time in one time zone to its equivalent in another time zone. The TimeZoneInfo class makes it possible to work with dates and times so that any date and time value unambiguously identifies a single point in time. The TimeZoneInfoclass is also extensible. Although it depends …
Example 1 – Basic Comparison
- In any case, here’s a quick example to demonstrate the basic difference between datetime and datetimeoffset. Result: Here, I set a datetime variable to the same value as the datetimeoffset variable. This causes the value to be converted to datetime and we can then use a SELECTstatement to see the value of each variable. In this case, the datetimeoffset value includ…
Example 2 – Setting Values from String Literals
- In the previous example, the datetime value was assigned by setting it to the same value as the datetimeoffsetvalue. When we do that, SQL Server performs an implicit conversion in order for the data to “fit” the new data type. If we try to assign the same value directly to the datetimevariable we get an error: Result: This is because the datetimedata type doesn’t support a string literal wit…
Example 3 – Storage Size
- The datetimedata type uses 8 bytes. The datetimeoffsetdata type uses either 8, 9, or 10 bytes, depending on its precision. Therefore, you’re not saving any storage size by using datetime. However, if you convert a datetimeoffsetvalue to a binary constant, it does add 1 byte in order to store precision. Here’s what happens if we use the DATALENGTH()...
Should I Use ‘Datetime’ Or ‘Datetimeoffset’?
- If you need to include a time zone offset, then you’ll need to use datetimeoffset. If not, then datetimemay suffice. However, Microsoft recommends that you use datetime2 for new work, as it has many benefits over datetime. See datetime vs datetime2for a comparison on these data types.