Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的日期格式转换#_C#_Asp.net_Datetime - Fatal编程技术网

C# c语言中的日期格式转换#

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

如何将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.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);