473,802 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TimeSpan.TotalD ays versus DateTime.ToOADa te difference

Given the example below, can someone explain why TimeSpan.TotalD ays gives a
different result than subtracting 2 DateTime.ToOADa tes?

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).TotalDay s : 0.0416666666666 667
(end.ToOADate() - start.ToOADate( )) : 0.0416666666642 413
((decimal)end.T oOADate() - (decimal)start. ToOADate()) : 0.0416666667

Code:

using System;
using System.Collecti ons.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.WriteLi ne("start: " + start);
Console.WriteLi ne("end : " + end);
Console.WriteLi ne();

TimeSpan duration = end - start;
Console.WriteLi ne("(end - start).TotalDay s
: " + duration.TotalD ays);

double totalDays = end.ToOADate() - start.ToOADate( );
Console.WriteLi ne("(end.ToOADa te() - start.ToOADate( ))
: " + totalDays);

decimal totalDays2 = ((decimal)end.T oOADate()) -
((decimal)start .ToOADate());
Console.WriteLi ne("((decimal)e nd.ToOADate() -
(decimal)start. ToOADate()) : " + totalDays2);

Console.ReadLin e();
}
}
}

Nov 18 '07 #1
1 9572
Da********@nosp am.nospam wrote:
Given the example below, can someone explain why TimeSpan.TotalD ays gives a
different result than subtracting 2 DateTime.ToOADa tes?

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).TotalDay s : 0.0416666666666 667
(end.ToOADate() - start.ToOADate( )) : 0.0416666666642 413
((decimal)end.T oOADate() - (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
7904
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 objects from the click of two buttons (start and stop) that I have placed on an ASP.NET webform. I have attempted to follow the many examples that are available on the web yet I'm finding that instead of returning the difference in minutes, my code is...
2
2420
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 - startTime)); //difference of ticks
6
31824
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 elapsed time and somehow convert the days to years w/out using the VB DLL? Thanks.
5
5894
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, VB 6.0, C/C++, Java etc. How I can send the DateTime from .NET Framework to all these other platforms. I heard that sending time in "double" format would be accepted by all the platforms.
2
19137
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 defined for the Date datatype. On the other hand, Dim Elapsed as DateTime = Now + Now does not give a compile time error but it thinks you're trying to
10
38680
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
2153
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 will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
4
2777
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 this much code so far. DateTime startDate = new DateTime("2008-02-04"); DateTime curDate = DateTime.Now(); TimeSpan ts = startDate.Subtract(curDate); Now I get lost. I was expecting to find a Days property off of the
2
12643
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
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.