C# c语言中的日期格式转换#
如何将2013年9月4日下午8:09:10转换为2013年4月9日(年月日)。我试图转换,但它以09为日期,04为月份 请帮忙C# c语言中的日期格式转换#,c#,asp.net,datetime,C#,Asp.net,Datetime,如何将2013年9月4日下午8:09:10转换为2013年4月9日(年月日)。我试图转换,但它以09为日期,04为月份 请帮忙 [HttpPost] public string getdailynote(DateTime selectedDate) //selectedDate gets date as 04/09/2013 8:09:10 PM { string x = selectedDate.ToString("MM-dd-yyy", CultureInfo.InvariantCu
[HttpPost]
public string getdailynote(DateTime selectedDate)
//selectedDate gets date as 04/09/2013 8:09:10 PM
{
string x = selectedDate.ToString("MM-dd-yyy", CultureInfo.InvariantCulture);
string daily;
DateTime dt = Convert.ToDateTime(x);
//dt gets value as {09/04/2013 12:00:00 AM} but 09 as date and 04 as month
Dnote.RealDate =dt;
daily = _scheduler.GetDailyNote(Dnote);
return daily;
}
它以09为日期,04为月份
是的,因为你说过,改变
"MM-dd-yyy"
到
现在,如果要将其转换为具有以下格式的字符串:MM dd yyyy
:
string result = dt.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);
使用
当您从DateTime
转换为string
时,可以使用实例方法ToString
的重载,在这里可以指定格式字符串,这很好
但是,当您向另一个方向转换时,不应该使用
convert.ToDateTime
方法。请改用DateTime.ParseExact
或DateTime.TryParseExact
,因为使用它们,您可以再次指定所需的格式字符串(以及格式提供程序(区域性))。请尝试selectedDate.ToString(“dd-MM-yyyy”)
question@SenthilKumar请标记问题,这将有助于了解更多信息,
string result = dt.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);
DateTime.ParseExact(x , "MM-dd-yyy", CultureInfo.InvariantCulture);