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();
}
}
} 1 9569 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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.
|
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.
|
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
| |
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.
|
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
|
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
|
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
};
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |