473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

TimeSpan.TotalDays versus DateTime.ToOADate difference

Given the example below, can someone explain why TimeSpan.TotalDays gives a
different result than subtracting 2 DateTime.ToOADates?

I am completely stumped. Thanks in advance,
Dan

Example Output:

start: 1/1/2007 12:00:00 AM
end : 1/1/2007 1:00:00 AM

(end - start).TotalDays : 0.0416666666666667
(end.ToOADate() - start.ToOADate()) : 0.0416666666642413
((decimal)end.ToOADate() - (decimal)start.ToOADate()) : 0.0416666667

Code:

using System;
using System.Collections.Generic;
using System.Text;

namespace DurationTest
{
class Program
{
static void Main(string[] args)
{
DateTime start = new DateTime(2007, 1, 1, 0, 0, 0);
DateTime end = new DateTime(2007, 1, 1, 1, 0, 0);

Console.WriteLine("start: " + start);
Console.WriteLine("end : " + end);
Console.WriteLine();

TimeSpan duration = end - start;
Console.WriteLine("(end - start).TotalDays
: " + duration.TotalDays);

double totalDays = end.ToOADate() - start.ToOADate();
Console.WriteLine("(end.ToOADate() - start.ToOADate())
: " + totalDays);

decimal totalDays2 = ((decimal)end.ToOADate()) -
((decimal)start.ToOADate());
Console.WriteLine("((decimal)end.ToOADate() -
(decimal)start.ToOADate()) : " + totalDays2);

Console.ReadLine();
}
}
}

Nov 18 '07 #1
1 9498
Da********@nospam.nospam wrote:
Given the example below, can someone explain why TimeSpan.TotalDays gives a
different result than subtracting 2 DateTime.ToOADates?

I am completely stumped. Thanks in advance,
Dan

Example Output:

start: 1/1/2007 12:00:00 AM
end : 1/1/2007 1:00:00 AM

(end - start).TotalDays : 0.0416666666666667
(end.ToOADate() - start.ToOADate()) : 0.0416666666642413
((decimal)end.ToOADate() - (decimal)start.ToOADate()) : 0.0416666667
DateTime and TimeSpan are 64 bit values which represent time in 1/10000
seconds.

When you convert this to a double value, you lose precision. A double is
also a 64 bit value, but only 52 bits are used for the actual data. You
only get nine to ten significant digits after the decimal point in an
OADate, so the last six digits that you see is just noise.

The conversion from double to decimal doesn't cause any further loss of
precision, but you can't get anything back that was lost in the first
conversion. The second and third results are equivalent, they are only
displayed differently because the decimal values keep track of how much
significant data there actually is.

--
Göran Andersson
_____
http://www.guffa.com
Nov 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Justin Grieves | last post by:
Hello all, I've been trying to determine the difference in minutes between two DateTime objects (startTime and endTime) in C# using the TimeSpan object (ts). I am populating the two DateTime...
2
by: KK | last post by:
Hi I was testing a quicksort algorithm. long startTime = DateTime.Now.Ticks; //ticks before sorting begins q_sort( 0, mElements.Length-1 ); TimeSpan tp =new TimeSpan((DateTime.Now.Ticks -...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
5
by: Naveen Mukkelli | last post by:
Hi, I'm writing a server applicaiton using C# and .NET Framework. This server sends out time to all the clients. The clients are expected to be written in various platforms for example, Delphi,...
2
by: DWalker | last post by:
In Visual Studio (Visual Basic) .NET 2002, I noticed that this: Dim Elapsed as DateTime = Now - Now gives a "compile time" error (error in the IDE), saying that the '-' operator is not...
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
4
by: kellygreer1 | last post by:
I haven't worked with the TimeSpan object before. So bare with me if this seems like a newb question. But if I wanted to know how many days from the current date until 2/4/2008. I have written...
2
by: shapper | last post by:
Hello, I have the following: boxStat = new BoxStat { BoxCount = database.Boxes.Count(), SinceLastCreate = (DateTime.Now - database.Boxes.Max(b => b.CreatedAt)).Days ?? 0 };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.