Difference between DateTime2 and DateTime data types?

A

Andy B

I was doing something with entity framework the other day and ended up with
the error "Conversion from DateTime2 to DateTime data type would result in a
loss of data..." What is the main differences between DateTime2 and DateTime
if they both hold the date and time?
 
G

Göran Andersson

Andy said:
I was doing something with entity framework the other day and ended up with
the error "Conversion from DateTime2 to DateTime data type would result in a
loss of data..." What is the main differences between DateTime2 and DateTime
if they both hold the date and time?

The DbType.DateTime2 enumeration value corresponds to a System.DateTime,
so it can handle any date from year 0 to year 9999, with a resolution of
0.0000001 seconds.

The DbType.DateTime enumeration value corresponds to SqlDbType.DateTime,
so it can handle any date from year 1753 to year 9999, with a resolution
of 0.003 seconds.

Converting from DateTime2 to DateTime would result in a loss of
precision, and could also cause an overflow.
 
M

Michel Posseth [MCP]

Hello Andy


..." What is the main differences between DateTime2 and DateTime if they
both hold the date and time?"

DateTime2 can hold more values as DateTime :

Minimum from midnight 1st January 1753 in datetime to 1st January 0000
Maximum from 11:59:59.997 PM 31st January 9999 to 11:59:59.9999999 PM 31st
January 9999.

Accuracy / Precission :

The increase in precision, from 3.33 milliseconds to 100 nanoseconds.

See this
http://msdn.microsoft.com/en-us/library/ms186724.aspx

And notice that although DateTime2 has advantages as a higher precission
and wider range it can have a lower storage size as a datetime value ( 6 - 8
bytes ) in comparisation to 8 bytes for a DateTime value.


Some people argue about the naming used "DateTime2" why not BigDateTime etc
etc etc , however the internal naming convention at MS for improved
functionality is the 2 suffix ( webbrowser control Navigate2 event etc etc
etc etc there are loads of examples to proove this )


HTH

Michel Posseth [MCP]
http://www.vbdotnetcoder.com/
http://www.linkedin.com/groups?gid=1770847&trk=hb_side_g
 
A

Armin Zingler

Andy said:
I was doing something with entity framework the other day and ended

Just jurious: What is an "entity framework"?
up with the error "Conversion from DateTime2 to DateTime data type
would result in a loss of data..." What is the main differences
between DateTime2 and DateTime if they both hold the date and time?

You are comparing apples and oranges. DateTime is a data type, whereas
DateTime2 is not, at least not in the Framework. DateTime2 is a member of
System.Data.SqlDbType, so it's a constant. I've searched in the MSDN docs
for datetime2 and found the explanation. You can do the same (so I don't
have to copy it all here).
Do you have Option Strict On? If not, you should, and start getting aware of
data types and how to handle and convert them.

It's more related to SQL server and the Framework, not to VB.


....If I read this http://msdn.microsoft.com/en-us/library/cc716729.aspx I
see it is mapped to the Framework data type datetime2, however, I don't know
how this can be true because I did not find it in the Framework.


Armin
 
M

Michel Posseth [MCP]

Armin Zingler said:
Just jurious: What is an "entity framework"?


You are comparing apples and oranges. DateTime is a data type, whereas
DateTime2 is not, at least not in the Framework. DateTime2 is a member of
System.Data.SqlDbType, so it's a constant. I've searched in the MSDN docs
for datetime2 and found the explanation. You can do the same (so I don't
have to copy it all here).
Do you have Option Strict On? If not, you should, and start getting aware
of
data types and how to handle and convert them.

It's more related to SQL server and the Framework, not to VB.


...If I read this http://msdn.microsoft.com/en-us/library/cc716729.aspx I
see it is mapped to the Framework data type datetime2, however, I don't
know
how this can be true because I did not find it in the Framework.


Armin


Well i just fixed on the part "what is the difference" with my previous
answer
but here is what i know for the rest of the part

LINQ to SQL and LINQ to Entities does support the new DateTimeOffset ,
DateTime2, and Date types (it requires SP1)

http://blogs.msdn.com/sbajaj/archive/2008/05/14/what-s-new-in-linq-to-sql-sp1.aspx

HTH

Michel
..
 
M

Michel Posseth [MCP]

Armin Zingler said:
Just jurious: What is an "entity framework"?


You are comparing apples and oranges. DateTime is a data type, whereas
DateTime2 is not, at least not in the Framework. DateTime2 is a member of
System.Data.SqlDbType, so it's a constant. I've searched in the MSDN docs
for datetime2 and found the explanation. You can do the same (so I don't
have to copy it all here).
Do you have Option Strict On? If not, you should, and start getting aware
of
data types and how to handle and convert them.

It's more related to SQL server and the Framework, not to VB.


...If I read this http://msdn.microsoft.com/en-us/library/cc716729.aspx I
see it is mapped to the Framework data type datetime2, however, I don't
know
how this can be true because I did not find it in the Framework.


Armin


Well i just fixed on the part "what is the difference" with my previous
answer
but here is what i know for the rest of the part

LINQ to SQL and LINQ to Entities does support the new DateTimeOffset ,
DateTime2, and Date types (it requires SP1)

http://blogs.msdn.com/sbajaj/archive/2008/05/14/what-s-new-in-linq-to-sql-sp1.aspx

HTH

Michel
..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top