简体   繁体   English

ASP.Net C#UTC / GMT日期时间

[英]ASP.Net C# UTC/GMT DateTime

I want get UTC 0 time which was Greenwich Mean Time (GMT). 我想获得格林威治标准时间(GMT)的UTC 0时间。

I try to DateTime.UtcNow and DateTime.Now.ToUniversalTime() but its returns wrong times. 我尝试使用DateTime.UtcNow和DateTime.Now.ToUniversalTime(),但是返回错误的时间。 İts return 22:40 instead of 06:40 返回22:40而不是06:40

How can I get UTC/GMT time in C#. 如何在C#中获取UTC / GMT时间。

I try to DateTime.UtcNow and DateTime.Now.ToUniversalTime() but its returns wrong times. 我尝试使用DateTime.UtcNowDateTime.Now.ToUniversalTime()但是返回错误的时间。

No, it really doesn't. 不,实际上不是。 (I would strongly advise using the former rather than the latter though. In general, converting from a local time to UTC can be ambiguous due to daylight saving transitions. I believe in this particular case it may do the right thing, but UtcNow is a better approach.) (我会强烈建议使用前者,而不是后者虽然在一般情况下,从本地时间为UTC转换可能会模棱两可,由于夏令时转换。我相信,在这个特殊的情况下,它可能会做正确的事情,但UtcNow是更好的方法。)

DateTime.UtcNow does return the current UTC time. DateTime.UtcNow 确实返回当前UTC时间。 If it's giving you the wrong result, then your system clock is wrong - or you're performing some other transformation of the value somewhere (eg converting it to local time as part of formatting). 如果给您错误的结果,则说明您的系统时钟有误-或您正在某处对值进行其他转换(例如,将其转换为本地时间作为格式化的一部分)。

Basically, you're using the right property, so you need to diagnose where exactly the result is going wrong. 基本上,您使用的是正确的属性,因此您需要诊断结果到底在哪里出错。

I suggest you start off with a small console app: 我建议您从一个小型控制台应用程序开始:

class ShowTimes
{
    static void Main()
    {
        Console.WriteLine("Local time: {0}", DateTime.Now);
        Console.WriteLine("UTC time: {0}", DateTime.UtcNow);
    }
}

What does that show, and what time zone are you in? 这显示什么,您处于哪个时区?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM