在asp.net中将dd / mm / yyyy转换为mm / dd / yyyy [英] converting dd/mm/yyyy to mm/dd/yyyy in asp.net

查看:81
本文介绍了在asp.net中将dd / mm / yyyy转换为mm / dd / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将dd / mm / yyyy日期格式转换为mm / dd / yyyy。在我的表单中,我想以dd / mm / yyyy为用户显示日期:

  protected   void  Calendar1_SelectionChanged( object  sender,EventArgs e)
{
TextBox2_PopupControlExtender.Commit(Calendar1.SelectedDate。 ToString( dd / MM / yyyy));
}



但是我们知道数据库接受mm-dd-yyyy中的日期,所以在执行我的项目时会产生错误:

Quote:

字符串未被识别为有效的DateTime。



我听说过某个地方我不得不首先拆分日期,然后将其更改为mm-dd-yyyy,但不知道该怎么做。 Thanx。

解决方案

数据库不接受mm-dd-yyyy中的日期,他们希望ISO格式为yyyy-MM-dd,但可以与其他人合作如果必须的话。



而不是传递字符串日期,而是使用参数化查询来传递DateTime对象 - 它更安全,因为字符串总是容易受到SQL注入攻击,并且更容易,因为它不需要您随时将DateTime转换为字符串并再返回。将日期格式化为字符串只应在最后时刻进行,以便呈现给用户,就像字符串应尽快转换为日期时间值一样 - 这样,您可以让用户以他的格式获取数据习惯了,并将他的计算机设置为。


这可以帮助你:

 DateTime.ParseExact(TextBox2.Text。 Trim(),  dd / MM / yyyy null )。ToString(  MM / dd / yyyy); 



检查这个 [ ^ ]了解更多详情。



- Amit


一个丑陋的解决方案,但它的工作原理?



 s = s.Split(   /)( 1 )+    / + s.Split(  /)( 0 )+   / + s.Split(  /)( 2 ); 


i want to convert dd/mm/yyyy date format to mm/dd/yyyy . In my form i want to show the date to user in dd/mm/yyyy for that i have done:

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
   {
       TextBox2_PopupControlExtender.Commit(Calendar1.SelectedDate.ToString("dd/MM/yyyy"));
   }


But as we know that database accepts date in mm-dd-yyyy, so on executing my project error is generated:

Quote:

String was not recognized as a valid DateTime.


I heard it somewhere that i had to split the date first and then change it to mm-dd-yyyy but don't know how to do it. Thanx.

解决方案

Databases don't "accepts date in mm-dd-yyyy", they expect ISO format "yyyy-MM-dd" but will work with others if they have to.

Instead of passing string dates around, use parametrized queries to pass DateTime objects instead - it's safer, because strings are always vulnerable to SQL Injection attack, and easier because it doesn't require you to convert DateTime to string and back again at any time. Formatting a date to a string should only be done at the last moment for presentation to the user, just as strings should be converted to datetime values as soon as possible - that way, you can let teh user have his data in the format he is used to, and has his computer set to.


This may help you:

DateTime.ParseExact(TextBox2.Text.Trim(), "dd/MM/yyyy", null).ToString("MM/dd/yyyy");


Check this[^] for more details.

--Amit


An ugly solution but it works?

s = s.Split("/")(1) + "/" + s.Split("/")(0) + "/" + s.Split("/")(2);


这篇关于在asp.net中将dd / mm / yyyy转换为mm / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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