简体   繁体   中英

Convert comma separated string to datetime

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)

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);

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM