开发者

Joining two DateTime's and getting the number of ticks

开发者 https://www.devze.com 2023-03-07 05:18 出处:网络
I\'m retrieving records from a database and it has two columns. One for Date and one for Time. Date looks like this: 19/05/2011 00:00:00 and Time looks like this: 01/01/1900 15:28:00 which is fine, I

I'm retrieving records from a database and it has two columns. One for Date and one for Time.

Date looks like this: 19/05/2011 00:00:00 and Time looks like this: 01/01/1900 15:28:00 which is fine, I can combine them easily with Date.Add(Time.TimeOfDay) and that gives me 19/05/2011 15:28:00.

If I try and get the "ticks" from that DateTime stamp with CombinedDateTime.Ticks it gives me the wrong one, e.g. 6344141544100000000 - it's like it's not finding the time. If I print the DateTime out it does show with the time and everything looks okay.

Date = new DateTime(2010, 05, 19);
Time = new DateTime(1900, 01, 01, 15, 45, 00);
Combined = Date.Add(Time.TimeOfDay);
// Combined.ToString() prints the expected 19/05/2011 1开发者_如何学JAVA5:45:00
// Combined.Ticks gives an invalid "tick count", e.g 6344141544100000000

Any ideas?

Thanks.


You're misunderstanding Ticks.

Ticks is a measured in units of 100 nanoseconds each; there are 10,000,000 ticks in a second.

There will always be zeroes at the end, unless your time as a fractional number of microseconds.


DateTime is immutable, when you invoke Add it creates a new DateTime instance. Are you invoking Ticks on the original DateTime?


Make sure you're updating your object when you combine them

Date = Date.Add(Time.TimeOfDay)

Date.Ticks;
0

精彩评论

暂无评论...
验证码 换一张
取 消