C#函数转换DateTime

Ish*_*han 0 c# asp.net datetime

我想转换一个字符串变量,其值为mm-dd-yyyy hh:mm:ss AM/PM to mm/dd/yy

我该怎么做?

Adr*_*der 5

首先使用DateTime.TryParseExact然后dateTimeVal.ToString("MM/dd/yy")

就像是

string stringDate = "12-31-2010 01:59:59 AM";
DateTime dateTimeValue;
if (DateTime.TryParseExact(stringDate, "MM-dd-yyyy hh:mm:ss tt", null, DateTimeStyles.None, out dateTimeValue))
{
    string newStringValue = dateTimeValue.ToString("MM/dd/yy");
}
Run Code Online (Sandbox Code Playgroud)