What is the difference between datetime.now and datetime.today
As an aside, to specify the ET timezone, I use import datetime. More details here. Eric Eric 1, 14 14 silver badges 24 24 bronze badges. Shep Shep 7, 3 3 gold badges 43 43 silver badges 65 65 bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Now; Now for the same scenario I described above, I get the value before the transition, or after the transition. You can get the current date for any time zone, including the local system time zone: using NodaTime; InZone zone1. InZone zone2. Date; If you don't want to use Noda Time, there is now another option.
Once added to your project, you will find you can do any of the following: using System; Improve this answer. Matt Johnson-Pint Matt Johnson-Pint k 67 67 gold badges silver badges bronze badges. What about the use of DateTime. UtcNow instead of DateTimeOffset. UtcNow is acceptable if you can convey in your application or spec that the value is in UTC.
I like to actually call the field or property something like MyDateUtc instead of just MyDate - but that's just icing on the cake. If you can't convey it in the spec or field name, then DateTimeOffset. UtcNow can be used to ensure the zero offset gets conveyed with the date and time values.
They are not equal. Today has the time as JamesWilkins - Not sure what you're getting at. So does DateTime. MattJohnson The question is asking the difference between DateTime. Today and DateTime. Now, not DateTime. Show 2 more comments. Today is essentially the same as. Marc Gravell Marc Gravell k gold badges silver badges bronze badges.
In particular, on a web app that's almost always the wrong choice. Now; return now. Guffa Guffa k gold badges silver badges bronze badges. Today represents the current system date with the time part set to and DateTime. Chris Moutray Today is DateTime. Now with time set to zero. So, using DateTime. Now and DateTime. Today is perfectly OK. Kobus Kobus 19 3 3 bronze badges. ToShortDateString will display only the date part. Daniel Mann When you run it on your computer, the time will differ.
Today is useful if you need to use a range of DateTimes starting on the current day. You could determine whether a piece of data was updated today or not by testing it against DateTime. We used the DateTime. Today property, which has an important difference from DateTime. Also, there are some places in this world such as Brazil where the "spring-forward" transition happens exactly at Midnight. The clocks go from to This means that the value you get for DateTime.
Today on that date, does not exist! Even if you use DateTimeOffset. Date , you are getting the same result, and you still have this problem. It is because traditionally, there has been no such thing as a Date object in.
So regardless of how you obtain the value, once you strip off the time - you have to remember that it doesn't really represent "midnight", even though that's the value you're working with. If you really want a fully correct solution to this problem, the best approach is to use NodaTime.
The LocalDate class properly represents a date without a time. You can get the current date for any time zone, including the local system time zone:. If you don't want to use Noda Time, there is now another option. I've contributed an implementation of a date-only object to the. Net CoreFX Lab project.
You can find the System. Time package object in their MyGet feed. Once added to your project, you will find you can do any of the following:.
Now includes the or whatever;. Today is the date-part only at on that day. So use. Now if you want to include the time, and.
0コメント