C#中UTC的本地日期和时间不正确 [英] Local date and time is incorrect from UTC in C #

查看:112
本文介绍了C#中UTC的本地日期和时间不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have inserted the date time as UTC format (2019-05-06T17:48:43Z)and while trying to return that and trying to display based on locale it is displaying incorrect date.
Could you please assit on this how to handle this type of utc format datetimes to convert as user local time zone.





我尝试了什么:



我尝试使用此代码时总是花费时间增加1小时。

实际摩洛哥时间:上午9:10

但此代码显示时间是上午10:10





What I have tried:

I have tried with this code it always get the time with increase of 1 hour.
Actual Morocco Time : 9:10 AM
But this code is displaying 10:10 AM

string utcLogTimeFromAPI = (String.Format("{0:s}", DateTime.UtcNow) + DateTimeOffset.UtcNow.ToString("%K"));
            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Morocco Standard Time");
            DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(utcLogTimeFromAPI).ToUniversalTime(), cstZone);
            Console.WriteLine("cstTime " + cstTime);

推荐答案

我怀疑Gerry是正确的:
I suspect that Gerry is correct:
引用:

看起来你正在将UTC转换为UTC

Looks like you're converting UTC to UTC



但唯一可以确定的是你 - 我们无法访问您的日志数据,因此我们不知道您正在阅读的内容。



所以,这取决于您。

幸运的是,您有一个工具可以帮助您了解正在发生的事情:调试器。一个快速谷歌的IDE和调试器的名称应该为您提供使用它所需的信息。



在函数的第一行放置一个断点,并通过调试器运行您的代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!


But the only person that can be sure is you - we don't have any access to your log data, so we have no idea exactly what you are reading.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for the name of your IDE and "debugger" should give you the info you need to use it.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于C#中UTC的本地日期和时间不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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