如何将日期转换为MM/dd/yyyy? [英] How to Convert date to MM/dd/yyyy?
本文介绍了如何将日期转换为MM/dd/yyyy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友..我正在尝试将日期转换为mm/dd/yyyy formate..i,我写了以下代码,但是代码不起作用...我不知道出了什么问题.请帮助我.谢谢.
>
字符串 fromdate = Gridtimesheetdetails.Rows [e.RowIndex] .Cells [ 2 ].Text.ToString() ;
DateTime dt1 = DateTime.ParseExact(fromdate," ,解决方案
具有格式的简单ToString应该可以完成Aysha的工作.
dt1.ToString(" );
希望能有所帮助.如果是这样,请标记答案.
谢谢
Milind
您不能将日期作为所需格式存储在datetime变量中;
您可以:
字符串 fromdate = Convert.ToDateTime(Gridtimesheetdetails.Rows [e.RowIndex] .Cells [ 2 ].Text).ToString(" );
或
字符串 fromdate = Gridtimesheetdetails.Rows [e.RowIndex] .Cells [ 2 ].Text.ToString(); 字符串 dt1 = Convert.ToDateTime(fromdate).ToString(" MM/dd/yyyy");
您尚未添加区域性.如果将提供者设置为null,则解析器方法将使用当前区域性.
你尝试过吗?
字符串 dateString = " ; CultureInfo ci = CultureInfo.InvariantCulture; DateTime myDate = DateTime.ParseExact(dateString," ,ci);
或使用您的代码:
CultureInfo ci = CultureInfo.InvariantCulture; 字符串 fromdate = Gridtimesheetdetails.Rows [e.RowIndex] .Cells [ 2 ].Text.ToString() ; DateTime dt1 = DateTime.ParseExact(fromdate," ,ci);
在我的系统上,当前区域性是de-DE,并且format = null,我得到了这种日期格式的异常,但是当我设置不变区域性时,它可以正确地解析日期.
>
hi friends..I m trying to convert my date in mm/dd/yyyy formate..i writtenn following code but code is not working...i dont know what is the problem.please help me.Thanks.
>
string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString();
DateTime dt1 = DateTime.ParseExact(fromdate, "MM/dd/yyyy", null);
解决方案
A simple ToString with format should do the job Aysha.
dt1.ToString("MM/dd/yyyy");
hope that helps. If it does, mark the answer.
Thanks
Milind
You can''t store date as your require format in datetime variable;
you can do:
string fromdate = Convert.ToDateTime(Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text).ToString("MM/dd/yyyy");
Or
string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString(); string dt1 = Convert.ToDateTime(fromdate).ToString("MM/dd/yyyy");
You did not add a culture. If you set the provider to null, the parser method uses the current culture.
Did you try this?
string dateString = "11/30/2012"; CultureInfo ci = CultureInfo.InvariantCulture; DateTime myDate = DateTime.ParseExact(dateString, "MM/dd/yyyy", ci);
Or with your code:
CultureInfo ci = CultureInfo.InvariantCulture; string fromdate = Gridtimesheetdetails.Rows[e.RowIndex].Cells[2].Text.ToString(); DateTime dt1 = DateTime.ParseExact(fromdate, "MM/dd/yyyy", ci);
On my system, current culture is de-DE and with format = null I get a exception with this date format, but whenn I set the invariant culture, it works an parses the date correct.
这篇关于如何将日期转换为MM/dd/yyyy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文