Convert comma separated string to datetime

吃可爱长大的小学妹 提交于 2020-01-04 13:45:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!