gpt4 book ai didi

c# - c#中的日期格式转换

转载 作者:行者123 更新时间:2023-11-30 19:41:51 25 4
gpt4 key购买 nike

如何将 04/09/2013 8:09:10 PM 转换为 09/04/2013 (MM-dd-yyyy)。我尝试转换,但它以 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;
}

最佳答案

it is taking 09 as date and 04 as month

是的,因为你说过,改

"MM-dd-yyy"

"dd/MM/yyyy h:mm:ss tt"

DateTime.ParseExact("04/09/2013 8:09:10 PM","dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

Demo

现在,如果你想把它转换成这种格式的字符串MM-dd-yyyy:

string result = dt.ToString("MM-dd-yyyy", CultureInfo.InvariantCulture);

关于c# - c#中的日期格式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com