C#和当前当地时间不同的国家 [英] C# and Current Local time for different countries

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

问题描述

C#提供了一种通过 DateTime.Now 获取当前日期的方法。但问题是我的服务器在美国,而当我使用 DateTime.Now

C# offers a way to get the current date by DateTime.Now. But the problem is my server is on US and I get the US time when I use DateTime.Now

时,我得到美国时间当我的用户来自世界各地时,有什么办法可以得到C#中特定国家/地区的当前时间? (我有每个用户的区域设置,所以如果我可以得到每个地区的当前时间,那么我是安全的)

Is there any way to get the current local time for a specific country in C# as my users are from around the world? (I have locale of each user, so if I Can get the current time for each locale then I’m safe)

我看到了一种方式使用时区,但是我必须保持时间区域与国家参考。

I've seen a way using Time Zone but then I have to keep time zone vs country reference.

我搜索过,而这里的大部分问题是将UTC时间转换为当地时间,反之亦然。希望有人能够启发我。

I’ve searched and most of the question here is about converting UTC time to local time and vice versa. Hope someone would be able to enlighten me.

推荐答案

使用UTC时间将您的时间保存在服务器中 DateTime.UtcNow)然后根据用户时区,您将更改此utc时间。不要在db

Save your time in your server using UTC time (DateTime.UtcNow) and then depending on the users time zone you will change this utc time. Never save local time in the db

这篇关于C#和当前当地时间不同的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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