DateTime.Now vs. DateTime.UtcNow

后端 未结 13 1512
暗喜
暗喜 2020-11-28 17:43

I\'ve been wondering what exactly are the principles of how the two properties work. I know the second one is universal and basically doesn\'t deal with time zones, but can

13条回答
  •  旧时难觅i
    2020-11-28 18:32

    It's really quite simple, so I think it depends what your audience is and where they live.

    If you don't use Utc, you must know the timezone of the person you're displaying dates and times to -- otherwise you will tell them something happened at 3 PM in system or server time, when it really happened at 5 PM where they happen to live.

    We use DateTime.UtcNow because we have a global web audience, and because I'd prefer not to nag every user to fill out a form indicating what timezone they live in.

    We also display relative times (2 hours ago, 1 day ago, etc) until the post ages enough that the time is "the same" no matter where on Earth you live.

提交回复
热议问题