Reputation: 9002
This is to resolve a difference in opinion in the office on what a full day consists of.
to represent a whole day (to the second) I would say:
2011/03/03 00:00:00 - 2011/03/04 00:00:00 = one full day.
eg:
TimeSpan test = new DateTime(2011, 03, 04, 00, 00, 00) - new DateTime(2011, 03, 03, 00, 00, 00);
The above code gives test a value of 1.00:00:00, therefore one full day.
Other opinion in the office:
2011/03/03 00:00:00 - 2011/03/03 23:59:59 = one full day.
If anyone can be bothered replying to this!! Which do they think is correct?
EDIT:
A few months has passed and the two people in the office with the "other opinion" are no longer employed here...
Upvotes: 9
Views: 3793
Reputation: 234384
You want one day, ask for one day:
TimeSpan oneDay = TimeSpan.FromDays(1);
The interval [2011/03/03 00:00:00, 2011/03/04 00:00:00[ spans one entire day. Notice that the upper bound is open. If your granularity is no less than one second, then that interval is exactly the same as [2011/03/03 00:00:00, 2011/03/03 23:59:59] (closed upper bound). Hope this clears up both points of view.
You can also say that you are thinking in terms of instants, and your coworkers are thinking in terms of one second periods. Imagine a long fence constructed with several wooden poles and each pair of consecutive poles is connected with a string of wire of the same length. You can say that the fence goes from pole 0 to pole 10, and your coworkers can say that the fence goes from string 0 to string 9.
Depending on what you're trying to do, both views can be correct. Using one of these points of view in a situation that requires the other is a fencepost error.
Upvotes: 38
Reputation: 3976
What about this:
var oneDay = TimeSpan.FromTicks(TimeSpan.TicksPerDay);
Here you have both: TimeSpan.FromTicks and TimeSpan.TicksPerDay
Upvotes: 0
Reputation: 179046
Let me pose this pseudo-graphical illustration of how differences work (subtraction).
Say you have a value on a line (3):
1 2 3 4
----+----+---(+)---+
and you want to know the distance from a different value (1):
1 2 3 4
---(+)---+----+----+
You subtract the two values, which will leave the points between the two values, as well as one of the endpoints (depending on the magnitudes and order of operations).
----+----+----+
subtract
----+
equals
----+----+
So, the correct way to calculate one day
is to take the difference between day 1 at 00:00:00
and day 2 at 00:00:00
. This is because the length that is the result will not include one of the endpoints.
day 1 00:00:00 <= X < day 2 00:00:00
-or-
day 1 00:00:00 < X <= day 2 00:00:00
Upvotes: 0
Reputation: 29164
One day is a certain duration (i.e. 24 hours, or 1440 Minutes or 86.400 Seconds). This duration is represented in .NET by the data type TimeSpan
.
The data type DateTime
on the other side represents a certain point in time which has no duration.
The duration can be measured between two points of time, so that the duration between 2011/03/03 0:00 and 2011/03/04 0:00 is exactly one day whereas the duration between 2011/03/03 0:00 and 2011/03/03 23:59:99 is one second less than a day.
Compare this with lengths: If you define points along a line in a distance of 1 meter and label them with '1','2','3',..., you (and probably your coworkers too) would readily agree, that 5 meter is the distance from point '1' to point '5' and not from point '1' to point '4' ...
Of course, if you want to count seconds, then you can define one day to consist of a certain number of seconds, 86400 to be exact. Now you can label each single second, the first being "2011/03/03 0:00:00", the second "2011/03/03 0:00:01" and so on. The day then consists of all the labeled seconds from "2011/03/03 0:00:00" until "2011/03/03 23:59:59". The label "2011/03/03 0:00:00" now no longer designates a point in time but a specific second, namly the one from 2011/03/03 0:00:00 until 2011/03/03 0:00:01. (I hope that the use of quotation marks is useful to understand the difference)
In this case you are of course bound to your discretization, i.e. you cannot express durations of less then a second. Also, if you would draw this on a time line, note that you would probably put the "second labels" in the middle of each second (instead of the beginning, where the point corresponding to the label lies): maybe this also helps to understand the difference visually.
Upvotes: 0
Reputation: 941277
The last moment of a day is at midnight minus the smallest measurable time increment. Which is up to you to define, it will be a second when you format it to a string. It will be 1/64th of a second for the operating system. It will be a fraction of a picosecond on NIST's atomic clocks.
It is 100 nanoseconds for the DateTime structure.
Upvotes: 4
Reputation: 11267
From a physical standpoint, time is continuous, and one could argue that the 2nd time (03/04 - 00:00:00) is part of the full 24 hour duration. However, this is a programming community, and therefore I'd argue that you have to think of time as something discrete. One day = lower bound to upper bound (both bounds included). If you let one full days upper bound equal to another days lower bound, you get an ambiguity for the case of only knowing that single boundary value.
So, in short, I'd argue that one full day in our real world is from 0:00 to 0:00, but in the discrete world of computers, you'd want it to last from 0:00 to 0:00 minus the lowest time delta you have access to, to avoid value clashing.
Upvotes: 0
Reputation: 158309
A full day consists of exactly 86400 seconds.
DateTime start = new DateTime(2011, 3, 3, 0, 0, 0);
DateTime earlyEnd = new DateTime(2011, 3, 3, 23, 59, 59);
Console.WriteLine((earlyEnd - start).TotalSeconds); // prints 86399
DateTime lateEnd = new DateTime(2011, 3, 4, 0, 0, 0);
Console.WriteLine((lateEnd - start).TotalSeconds); // prints 86400
The confusing part here is that a DateTime
represents an exact instant. The full day today lasts from the instant that the time becomes 00:00, March 3, until the exact instant that the time becomes 00:00, March 4. The instant does not have any length, but I think our minds have some trouble thinking about time but at the same time not giving it any length.
From a more philosophical point of view, we could perhaps say that the above code sample will calculate the time from the beginning of a certain second, to the beginning of a certain other second. That makes it clear that the first second is included in the calculated time span, but the last one is not.
Upvotes: 15
Reputation: 56457
As a side note, if you want the time interval of a specific day, be aware that not all days have 24 hours.
Some days have leap seconds, which makes them one second longer.
Daylight savings time changes make days have 23 or 25 hours (depending on the change).
Upvotes: 1
Reputation: 133975
You and your colleagues are approaching the problem from two different perspectives, and from your individual perspectives you're both right.
A full day is 86,400 seconds. The day covers the span from 2011-03-02 00:00:00 to 2011-03-02 23:59:59, inclusive. That's 86,400 seconds. But to compute the span of a single day, you have to subtract 2011-03-02 00:00:00 from 2011-03-03 00:00:00.
This is no different from saying that the range [0-9] contains 10 items. But if you subtract 0 from 9 you get 9. Huh? Oh, right, if you want to compute how many items are in a range, you have to subtract the beginning range value from the ending range value and then add one.
Upvotes: 3
Reputation: 12362
Try not to think of it as time, instead as digits.
Your question is about 2 - 1
and 2.0 - 0.9
. What do you expect?
This is simple basic elementary math at first grade level.
Upvotes: 0
Reputation: 10600
Is 59 minutes an hour? Are 23 hours, 59 minutes and 59 seconds a day?
That said, I know why your coworkers have the opinion they do. Date comparison is tricky.
Upvotes: 1
Reputation: 10561
This is an interesting question. I guess the answer depends on your application. If you use the first method, you will count one second twice.
Upvotes: 1
Reputation: 995
A day is 24 hours. A day is not 23 hours, 59 minutes, and 59 seconds.
Upvotes: 4
Reputation: 132984
Obviously if the first approach returns 1.00:00:00 and the second returns 0.23:59:59 then the first one is correct (it reads 1 day while the second is smaller than 1 day)
Upvotes: 2
Reputation: 2731
the first option is correct as you miss exactly the second in between 23:59:59 and 0:00:00 of the following day in the second option...
Some people may take the second option in order to make clear that they mean that the date of the 3rd of March must be shown. However, in that case, 23:59:59,99 also belongs to the day (and it will be shown as 23:59:59 in a lot of software) and so on. So, in the end, this converges to the first option again ;)
Upvotes: 3