Din*_*esh 0 c# string datetime
这个功能工作正常,但我返回dd-MM-yyyy格式,但我想要yyyy-MM-dd格式
我的输入值是'13/5/2014 12:00:00 AM'我需要将此格式更改为'2014-5-13 00:00:00',但所有datetime变量都以dd-mm-yyyy格式返回I不希望将日期转换为字符串我想在datetime属性中使用'yyyy-MM-dd'格式存储日期值:
public DateTime DateConvertion(string Input)
{
DateTime DateValue = DateTime.ParseExact(Input, "dd-MM-yyyy", CultureInfo.InvariantCulture);
return DateValue;
}
Run Code Online (Sandbox Code Playgroud)
使用指定的格式和特定于区域性的格式信息将指定的日期和时间字符串表示形式转换为其DateTime等效形式.字符串表示的格式必须与指定的格式完全匹配.
在你的情况下,他们不是.
您可以改用dd/M/yyyy hh:mm:ss tt
格式.这是一个例子;
string s = "13/5/2014 12:00:00 AM";
var date = DateTime.ParseExact(s, "dd/M/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Run Code Online (Sandbox Code Playgroud)
DateTime
没有隐式格式,它只是一个DateTime
值.您可以像string
使用DateTime.ToString()
方法一样格式化它;
date.ToString("yyyy-M-dd hh:mm:ss");
Run Code Online (Sandbox Code Playgroud)
看一眼;