Converting string format to datetime in mm/dd/yyyy

前端 未结 6 1064
萌比男神i
萌比男神i 2020-11-27 18:39

I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format.

string strDate = DateTime.Now.ToString(\"MM/dd/         


        
6条回答
  •  旧巷少年郎
    2020-11-27 19:38

    You are looking for the DateTime.Parse() method (MSDN Article)

    So you can do:

    var dateTime = DateTime.Parse("01/01/2001");
    

    Which will give you a DateTime typed object.

    If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)

    Which you would use in a situation like this (Where you are using a British style date format):

    string[] formats= { "dd/MM/yyyy" }
    var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
    

提交回复
热议问题