DateTime.Now 与 DateTime.UtcNow [英] DateTime.Now vs. DateTime.UtcNow

查看:39
本文介绍了DateTime.Now 与 DateTime.UtcNow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道这两个属性的工作原理究竟是什么.我知道第二个是通用的,基本上不涉及时区,但有人可以详细解释它们是如何工作的,哪个应该在什么场景下使用?

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 someone explain in detail how they work and which one should be used in what scenario?

推荐答案

DateTime.UtcNow 告诉您协调世界时的日期和时间,也称为格林威治标准时区 - 基本上就像您在英国伦敦时一样,但不是在夏天.DateTime.Now 给出日期和时间在您当前的语言环境中显示给某人.

DateTime.UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime.Now gives the date and time as it would appear to someone in your current locale.

我建议在您向他人显示日期时使用 DateTime.Now - 这样他们就会对他们看到的价值感到满意 - 他们可以轻松地进行比较他们在手表或时钟上看到的东西.使用 DateTime.UtcNow 当您想要存储日期或将它们用于以后的计算时(在客户端 - 服务器模型中)您的计算不会被来自您的服务器的不同时区的客户端混淆,或者来自彼此.

I'd recommend using DateTime.Now whenever you're displaying a date to a human being - that way they're comfortable with the value they see - it's something that they can easily compare to what they see on their watch or clock. Use DateTime.UtcNow when you want to store dates or use them for later calculations that way (in a client-server model) your calculations don't become confused by clients in different time zones from your server or from each other.

这篇关于DateTime.Now 与 DateTime.UtcNow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆