What is the difference between date and datetime




















How do we know that if EndTime is "AM" we need assume is tomorrow in the code? If EndTime is "PM", we assume is today? We mean how to tell is "AM" or "PM". That's precisely the issue you need to address - it's part of the data validation that should occur before you do the calculation. You might decide that it should not happen and the result cannot be calculated, or you might decide that if it does happen then you assume next day, However, even that might be wrong if it should actually be next working day for instance, if you are trying to estimate the delivery time for a parcel.

It all depends on the details of your application. One possibility is to do the calculation assuming that both times are on the same day. If that results in a negative value, then re-do the calculation assuming that the end date is one day later. So if you want to select on a time then return the 24 hour of the day and check if it fits between your rules for day or evening. We actually use DateTime. TotalMinutes to get each day total minutes and get absolute value from those two minutes:.

You are making the assumption that if the end time is later than the start time then the end time and start time are in the same day, but if the end time is earlier then the start time then the end time must be a time within the next day. Whether or not that is a reasonable assumption depends on what that time data is and how you are using the calculation in your application.

If the two times actually indicate a total timespan of more than 24 hours then your assumption is wrong and the calculation is incorrect. Date is a keyword in VB which represent DateTime. But Date is also a property of DateTime which represents a Date without a time.

According to this [and if I understand well : ] Date is a type although it's a keyword too which aliases DateTime and its' Date property my bad for using field returns a DateTime which represents its' Date component and discarding his Time component a. Saying it doesn't have a Time is IMO an error, it's like saying an int whose value is 0 doesn't have a value. That is precisely the issue I was referring to, although it arises as a problem with dates much less frequently than with times.

You cannot use a DateTime to compare or calculate two times without being clear about the date to which each one applies. Similarly, you cannot compare or calculate two dates without ensuring that the times are set appropriately. Even though the time is often not relevant, user should always ensure that it is set to some known value, such as zero.

But zero is still a time, and can't be just assumed or ignored. Learn More. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. It might still help to keep a secondary column of the native type to use for range queries, depending on your needs. Great discussion, BTW. I'll index and utilize a datetime 6 field for queries, and accurately store the value presented to us in a varchar. Show 4 more comments. Active Oldest Votes.

Improve this answer. Saty Saty Anthony No. BenJohnson fair enough. Show 5 more comments. Booboo Booboo Excellent point. Note that Timestamp can only handle dates through , whereas DateTime can handle years through Timestamp's primary use is for datetimes reasonably near the present. Keep in mind that problems don't wait for to happen, they can happen right now - for example, if I have a database of retirement funds which have a year-target-horizon and I want to store that date, it will already wrap around and very bad things will happen.

Real life example of problems already happening related to Y2K38 — Tim. Tim: Good point. Clearly if you need to work with dates that go to and beyond, then the only option is to use DATETIME types and consistent session time zone settings when initializing, displaying and comparing.

I will add an update to my answer. Booboo It's a great answer and I appreciate you posting it - just want to make clear to anybody stopping by the danger of and that it's happening now. I wish we had support for a field with time zone independence and post support! I guess one option is to use datetime and convert to UTC before inserting, but that's more error-prone on the client side — Tim.

Community Bot 1 1 1 silver badge. Hossein Mobasher Hossein Mobasher 4, 5 5 gold badges 40 40 silver badges 67 67 bronze badges. Add a comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. The above result is subject to change to the set time zone, i.

Next Recommended Reading. Windows 10 Vs Windows Visual Studio Vs Visual Studio Understanding Matplotlib With Examples. Understanding Numpy With Examples. C Evolution.



0コメント

  • 1000 / 1000