Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Now Returned Weird Date

Tags:

c#

.net

I have a WinForms application that was written in C# .NET 3.5. This application interacts with a SQL Server 2008. Whenever I add a record into the database, I have a DateAdd column that I insert DateTime.Now into. For whatever reason, I have 4 records with odd dates:

'1980-01-03 23:08:43.970'    
'1980-01-03 23:08:44.157'    
'1980-01-03 23:08:44.530'    
'1980-01-03 23:08:45.547'

The records before and after these all have proper dates of '2011-05-29 XX:XX:XX.XXX'. Users have no access to modify the date fields in any application.

Is there any reason that the dates would change like this? Users have no access to modify their system time, which I'm assuming is where DateTime.Now gathers the date from.

Basically, I've come to the conclusion that there is either a bug, or a user has a Delorean fully equipped with a flux capacitor...

like image 738
Aaron Avatar asked May 30 '11 15:05

Aaron


2 Answers

If this is a client windows app, check the system time on the machines that the users in question are using. Also, I don't know if this is best practice or not, but I find it easier to maintain if I use GetDate() in SQLSERVER rather than passing the date from the C#.NET application in cases were I need to record the current time.

like image 126
Jonathan Henson Avatar answered Sep 18 '22 02:09

Jonathan Henson


The motherboard battery on one of your users computers is dead - look for the "Windows 98 Compatible" sticker on the front of a (now) yellow Compaq desktop under someones desk. That computer was powered on 2 days before writing a record to your database. The next time it is completely shutdown or power cycled it will go back to Jan 1, 1980.

Best practice would be to have the database assign the date (set the column default as GETDATE()). This way a single date scheme is used and you don't have to worry about users changing dates, events from different users appearing in the wrong order because their computers where a few minutes off one another, or the times being several hours off because of incorrect timezone settings.

like image 22
David Avatar answered Sep 22 '22 02:09

David