问题
The Exchange webservice has a method that takes the DateTime in the format below
appointment.Start = new DateTime(2014, 03, 04, 11, 30, 00);
I have a string which is formed by concatenating various fields to form the date my string is as below:
string date="2014,03,04,11,00,00"
But if i try to to parse my string as the date it gives the error "String was not recognized as a valid DateTime".
DateTime.Parse(date)
回答1:
You can use DateTime.ParseExact
:
string date = "2014,03,04,11,00,00";
DateTime dateTime = DateTime.ParseExact(date, "yyyy,MM,dd,HH,mm,ss", CultureInfo.CurrentCulture);
回答2:
Try this :
string date = "2014,03,04,11,00,00";
DateTime datDate;
if(DateTime.TryParseExact(date, new string[] { "yyyy,MM,dd,hh,mm,ss" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datDate))
{
Console.WriteLine(datDate);
}
来源:https://stackoverflow.com/questions/21548184/convert-comma-separated-string-to-datetime