c# - Difference between System.DateTime.Now and System.DateTime.Today -


can explain difference between system.datetime.now , system.datetime.today in c#.net? pros , cons of each if possible.

datetime.now returns datetime value consists of local date , time of computer code running. has datetimekind.local assigned kind property. equivalent calling of following:

  • datetime.utcnow.tolocaltime()
  • datetimeoffset.utcnow.localdatetime
  • datetimeoffset.now.localdatetime
  • timezoneinfo.converttime(datetime.utcnow, timezoneinfo.local)
  • timezoneinfo.converttimefromutc(datetime.utcnow, timezoneinfo.local)

datetime.today returns datetime value has same year, month, , day components of above expressions, time components set zero. has datetimekind.local in kind property. equivalent of following:

  • datetime.now.date
  • datetime.utcnow.tolocaltime().date
  • datetimeoffset.utcnow.localdatetime.date
  • datetimeoffset.now.localdatetime.date
  • timezoneinfo.converttime(datetime.utcnow, timezoneinfo.local).date
  • timezoneinfo.converttimefromutc(datetime.utcnow, timezoneinfo.local).date

note internally, system clock in terms of utc, when call datetime.now first gets utc time (via getsystemtimeasfiletime function in win32 api) , converts value local time zone. (therefore datetime.now.touniversaltime() more expensive datetime.utcnow.)

also note datetimeoffset.now.datetime have similar values datetime.now, have datetimekind.unspecified rather datetimekind.local - lead other errors depending on it.

so, simple answer datetime.today equivalent datetime.now.date.
but imho - shouldn't use either 1 of these, or of above equivalents.

when ask datetime.now, asking value of local calendar clock of computer code running on. not have information clock! best datetime.now.kind == datetimekind.local. local it? information gets lost value, such store in database, display on screen, or transmit using web service.

if local time zone follows daylight savings rules, not information datetime.now. in ambiguous times, such during "fall-back" transition, won't know of 2 possible moments correspond value retrieved datetime.now. example, system time zone set mountain time (us & canada) , ask datetime.now in hours of november 3rd, 2013. result 2013-11-03 01:00:00 mean? there 2 moments of instantaneous time represented same calendar datetime. if send value else, have no idea 1 meant. if in time zone rules different.

the best thing use datetimeoffset instead:

// unambiguous. datetimeoffset = datetimeoffset.now; 

now same scenario described above, value 2013-11-03 01:00:00 -0600 before transition, or 2013-11-03 01:00:00 -0700 after transition. looking @ these values can tell meant.

i wrote blog post on subject. please read - the case against datetime.now.

also, there places in world (such brazil) "spring-forward" transition happens @ midnight. clocks go 23:59 01:00. means value datetime.today on date, does not exist! if use datetimeoffset.now.date, getting same result, , still have problem. because traditionally, there has been no such thing date object in .net. regardless of how obtain value, once strip off time - have remember doesn't represent "midnight", though that's value you're working with.

if want correct solution problem, best approach use nodatime. localdate class represents date without time. can current date time zone, including local system time zone:

using nodatime; ...  instant = systemclock.instance.now;  datetimezone zone1 = datetimezoneproviders.tzdb.getsystemdefault(); localdate todayinthesystemzone = now.inzone(zone1).date;  datetimezone zone2 = datetimezoneproviders.tzdb["america/new_york"]; localdate todayintheotherzone = now.inzone(zone2).date; 

if don't want use noda time, there option. i've contributed implementation of date-only object .net corefx lab project. can find system.time package object in myget feed. once added project, find can of following:

using system; ...  date localdate = date.today;  date utcdate = date.utctoday;  date tzspecificdate = date.todayintimezone(anytimezoneinfoobject); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -