如何在csharp.net中隐蔽日期时间 [英] How to covert dat time in csharp .net

查看:96
本文介绍了如何在csharp.net中隐蔽日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Frnds,

任何人都知道如何将日期时间从(27/10/2011)转换为(2010/10/27)


我需要插入数据库表(sql格式(yyyy/mm/dd).



请在这种情况下的任何人都很好地帮助我

解冻
Venkat.S

Hi Frnds,

any one knows how to convert date time from (27/10/2011) to (2010/10/27)


i need to insert in to database table(sql format (yyyy/mm/dd).



please any one knows well in this scenario help me

Thaks
Venkat.S

推荐答案

可以使用ToString("format")重载将日期转换为任何格式的字符串,例如date.ToString("yyyy-MM-dd").

如果某些数据库引擎对"/"存在问题,请使用-"作为分隔符,而不是"/"作为日期.
A date can be converted to any format string with the ToString("format") overload for example date.ToString("yyyy-MM-dd").

Use ''-'' as a separator instead of ''/'' for dates as some database engines have problems with ''/''.


如果要在C#中执行此操作.Net,请使用以下语法.
If you want to do this in C#.Net, use below syntax.
string myDate = DateTime.Now.ToString("yyyy/MM/dd");


如果您想在SQL中执行此操作,请查看下面的链接.
http://www.w3schools.com/sql/func_convert.asp


In case you want to do this in SQL, then have a look at below link.
http://www.w3schools.com/sql/func_convert.asp


首先要做的是:不要将日期转换为任何字符串格式-而是将其转换为DateTime值,并将其直接存储在数据库中(在SQL DateTime字段中).您可以将其作为参数传递,因此不需要进行转换.

将字符串转换为日期时间很容易:
The first this to do is: do not convert the date to any string format - instead, convert it to a DateTime value and store that directly in the database (in a SQL DateTime field). You can pass it in as a parameter, so it doesn''t need to be converted.

It is easy to convert a string to a datetime:
DateTime dt = DateTime.Parse(dateAsString);

考虑了用户在PC上应用的任何日期格式设置,因此您不必担心自己.它还具有的优势是,此时会捕获任何无效的日期,然后再将它们输入数据库以引起以后的问题.

将其作为DateTime存储在数据库中意味着可以更轻松地进行比较,也可以在以后检索信息时将其格式化为用户PC设置.

This takes into account whatever date format settings the user has applied on his PC, so you don''t have to concern yourself. It also has the advantage that any invalid dates are caught at this point, before they enter your database to cause problems later.

Storing it as a DateTime in the Database means that it is easier to use in comparisons, and also to format to the user PC settings when you retrieve the info later.


这篇关于如何在csharp.net中隐蔽日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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