以我的国家/地区格式本地化日期和时间 [英] Localizing Date and time in my country Format

查看:64
本文介绍了以我的国家/地区格式本地化日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用(DateTime.Now())方法将日期保存在我网站的数据库中,
但是服务器(GoDaddy)以其国家/地区格式(MM/DD/YYYY)保存日期和时间

我使用CultureInfo类将其日期格式转换为(DD/MM/yyyy)(它在我的本地主机中正常工作),但是在站点部署时抛出了异常(我不知道为什么吗?)

有没有一种解决方案可以本地化我的服务器,以我的国家/地区格式保存日期和时间?

i''m using (DateTime.Now()) method to save date in database of my site,
but server (GoDaddy) saving date and time in it''s country format (MM/DD/YYYY)

i used CultureInfo class to convert it''s date format to (DD/MM/yyyy) (it''s working properly in my localhost) but its thrown an exception when site deployed( i don''t know why ? )

is there a solution to localize my server to save date and time in my country format ??

推荐答案

您应该使用时间,而不要使用表示时间的字符串.您需要花费一些时间,直到您需要在屏幕上显示字符串的那一刻为止.甚至可以避免这种情况,因为您可以使用诸如DateTimePicker这样的控件: DateTimePicker Web控件 [ ^ ].

现在,CultureInfo与时区无关.它也与时间的字符串表示形式有关.

为避免混淆,您可以将所有时间都放在UTC时区的服务器端,这将为您提供相同的通用时间,无论您的系统在哪里.使用System.DateTime.UtcNow代替System.DateTime.UtcNow.这样,您就无需考虑本地时间(服务器所在的位置).当然,服务器上的本地时钟应正确设置为本地时间.现在,您需要呈现的是当地时间是某个时区.为此,可以使用DateTimeOffsetTimeZoneInfo.

使用它的所有必需的详细信息在这里说明: http://msdn.microsoft.com/en-us/library /bb384267.aspx [ ^ ].

表格更多信息,请参见:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.timezone.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx [ ^ ].


您需要使用带有参数(格式或区域性)的System.DataTime.ToString方法之一以所需的格式保存System.DateTime.您将在这里看到代码示例: http://msdn.microsoft.com/en-us/library/zdtaw1bw .aspx [^ ] .

—SA
You should work with time, not with strings representing time. You need to work with time until the moment you need to present is as a string on screen. Even this can be avoided as you can use some control such as this DateTimePicker: DateTimePicker Web Control[^].

Now, CultureInfo has nothing to do with time zone; it is also related to string representation of the time.

To avoid confusion, your can get all time on server side in UTC timezone which will give you the same universal time no matter where you system is. Use System.DateTime.UtcNow instead of System.DateTime.UtcNow. This way, you removed local time (where the server is) from consideration. Of course, the local clock on the server should be set correctly to the local time. Now, you need to present is as a local time is some time zone. To do that, you can use DateTimeOffset and TimeZoneInfo.

Using it explained in all required detail here: http://msdn.microsoft.com/en-us/library/bb384267.aspx[^].

Form more information, see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timezone.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx[^].


You need to save System.DateTime in required format using one of the System.DataTime.ToString methods with parameters (format or culture). You will see code samples here: http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^].

—SA


这篇关于以我的国家/地区格式本地化日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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