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

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

问题描述

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

解决方案

DateTime.UtcNow 告诉你日期和时间,因为它将在协调世界时,这也被称为格林威治标准时间时区 - 基本上就像是在英国伦敦,但不是在夏天。 DateTime.Now 给出日期和时间,因为它会对您当前的区域设置中的某人显示。



每当向人类显示日期时,建议使用 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 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.

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天全站免登陆