.net c# DateTime.Now给不正确的时间

DateTime.Now giving incorrect time
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我正在使用C#在Visual Studio中开发应用程序.创建对象的新实例时,我想将当前日期和时间附加到该对象上.使用DateTime.Now时间减少了一个小时.

我认为它是时区或夏时制的问题.我曾尝试过致电System.Globalization.CultureInfo.CurrentCulture.ClearCachedData(),DateTime.Now但得到了相同的结果.我该如何解决?

速聊2:
DateTime.Now根据计算机的区域性返回当前本地时间.您想获得哪种日期?本地?世界标准时间?
速聊4:
它是一个网站应用程序,允许您添加更新.所有更新都可以显示为列表,该列表显示添加更新的时间/日期.当我添加新更新时,时间缩短了一个小时.
速聊5:
然后在我看来,您需要放弃使用当地时间.在内部使用UTC,并在要显示时将其转换为特定时区(而不是服务器的配置系统时区)?
解决过程1

您应该使用DateTime.UtcNow而不是DateTime.Now避免时区问题(DateTime.Now使用服务器的时区),然后可以将UTC值转换为用户的时区.

I'm developing an application in Visual Studio using C#. When I create a new instance of an object I want to attach the current date and time to it. Using DateTime.Now the time is off by one hour.

I presume its a time-zone or daylight saving issue. I tried calling System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() right before DateTime.Now but got the same result. How do I resolve this?

Talk1:
Talk2:
DateTime.Now returns the current local time based on the culture of the machine. What type of date are you trying to get? Local? UTC?
Talk4:
Its a website application that allows you to add updates. All updates can be displayed as a list which shows the time/date the update was added. When I add a new update the time is off by one hour.
Talk5:
Then it sounds to me like you'll need to give up on using local time. Use UTC internally, and convert it to a specific time zone (not the server's configured system time zone) when you want to display it?
Solutions1

You should use DateTime.UtcNow instead of DateTime.Now to avoid time zone issues (DateTime.Now uses the time zone of the server), then you can convert the UTC value to the user's time zone.

转载于:https://stackoverflow.com/questions/23712404/datetime-now-giving-incorrect-time

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入