更改日期格式 [英] changing of date format

查看:83
本文介绍了更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种将数据保存到sql db中的表格.日期保存在数据库中,如
2012-10-25 00:00:00:000.如何更改日期格式,以便将其保存到
仅限25-10-2012这样的数据库(dd.mm.yyyy)
感谢

i have a form which saves data into sql db. the date is saved in the database like
2012-10-25 00:00:00:000. how can i change the date format so it saves into the
database like 25-10-2012 only (dd.mm.yyyy)
thanks

推荐答案

只需添加到Marcus所说的内容,从字符串到DateTime的转换应该在物理上尽早完成,而从DateTime到字符串的转换应尽可能早地完成.尽可能晚些做.

这样做的原因与本地化有关-从字符串转换的越早,您越有可能能够访问用户设置的语言环境信息,并且越有可能转换正确且正确.最终结果是用户想要输入的内容.
例如,如果用户输入12/10/11,他的意思是2012年11月10日,2012年10月11日或2010年11月12日?如果您有权访问语言环境信息,则可以确定并正确解析字符串.

转换回字符串的时间越晚,您越有可能为用户生成可理解的日期-该用户的语言环境和日期格式可能会完全不同.

请记住,DateTime不会像这样存储日期或时间,而是将它存储为自固定时间点以来的间隔(以毫秒为单位).因此,参考用户期望的语言环境转换为字符串或从字符串转换为字符串,意味着即使约会系统不是格里高利安,日期也可以是正确的.例如,Hirji使用的是穆罕默德从麦加移民到麦地那的农历年. > 今天的公历日期是2012年10月25日,大约是赫尔希(Hirji)9 Thw al-Hijjah1433.
正如我所描述的那样进行转换意味着这根本不需要使您的软件复杂化.
Just to add to what Marcus says, conversion from a string to DateTime should be done as early as physically possible, and conversion from a DateTime to a string should be done as late as possible.

The reasons for this are to do with localisation - the earlier you convert from a string, the more likely you are to be able to access the locale information that the user has set, and the more likely you are that the conversion will be correct and the end result what the user intended to enter.
For example, if the user enters 10/11/12 does he mean 10th November 2012, 11th Oct 2012, or 12th November 2010? If you have access to the locale information you can tell and parsing of the string can be done correctly.

The later you convert back to a string, the more likely you are to generate an understandable date fro teh user - who may have a totally different locale and date format.

Remember that DateTime does not store a date or time as such - it stores it as an interval in milliseconds since a fixed point in time. So conversion to or from a string with reference to the locale the user is expecting means that date can be correct even if the dating system is not Gregorian - Hirji for example uses the lunar year in which Muhammad immigrated from Makkah to Madinah.
The Gregorian date for today 25 Oct 2012 is (approximately) 9 Thw al-Hijjah 1433 in Hirji.
Converting as I describe means this doesn''t have to complicate your software at all.


简单的答案是,您不需要.
如果您想随时在数据库中进行日期计算,则将字段定义为此类.
当您将数据拉回到C#时,只需格式化日期就可以了.
日期时间格式C# [
The simple answer is, you don''t.
If you want at any time to do date calculations within the database keep the fields defined as such.
When you pull your data back into C#, just format the date as you wish it to appear.
DateTime Formatting C#[^]


这篇关于更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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