C #: DateTime conversion

I get the date in the format:

18/04/2011 4:57:20 PM

The method DateTime.Parse()does not get access to it.

Is there a way to make it convert 18/04/2011 4:57:20 PMto return an object Date April 18, 2011?

+5
source share
8 answers

This is not like the standard format. Date en-GB, time en-US. Because of this, I would recommend that you use DateTime.ParseExact and pass in the format:

DateTime parsed = DateTime.ParseExact("18/04/2011 4:57:20 PM", 
                                      "dd/MM/yyyy h:mm:ss tt", 
                                      CultureInfo.InvariantCulture);
+9
source

First of all, to change the date format, you need a value DateTime. You cannot format the date of a string. Use DateTime.ParseExact to extract your date value from a formatted date string:

DateTime dateValue = 
   DateTime.ParseExact(stringDateValue, "dd/MM/yyyy h:mm:ss tt", 
        CultureInfo.InvariantCulture);

DateTime.ToString(format), :

resultStringDateValue = dateValue.ToString("MMM dd, yyyy");
+7

:

DateTime dt = DateTime.ParseExact(dateString, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
+3

ParseExact TryParseExact ( ), :

DateTime.ParseExact(strDate, "dd/MM/yyyy HH:mm:ss", null);
+3

Convert.ToDateTime(String)

http://msdn.microsoft.com/es-es/library/xhz1w05e(v=VS.90).aspx

, Convert.ToDateTime , .

+1

DateTime.Now.ToString("MMM dd, yyyy")
0

- mm/dd/yyyy, 18

DateTime.Parse("04/18/2011 4:57:20PM").ToShortDateString();
0

All Articles