C#: DateTime convert
I am recieving a date in the format:
18/04开发者_如何学C/2011 4:57:20 PM
The DateTime.Parse()
method does not access it.
Is there a way to get it to convert 18/04/2011 4:57:20 PM
to return a Date
object April 18, 2011
?
This doesn't look like a standard format. The date is en-GB, the time en-US. Because of this, I would recommend you use DateTime.ParseExact and pass the format:
DateTime parsed = DateTime.ParseExact("18/04/2011 4:57:20 PM",
"dd/MM/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
First of all, to change the format of a date, you need a DateTime
value. You can not format a string date. Use DateTime.ParseExact to extract your date value from a formated date string:
DateTime dateValue =
DateTime.ParseExact(stringDateValue, "dd/MM/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Then you can use DateTime.ToString(format) to extract a formated string value:
resultStringDateValue = dateValue.ToString("MMM dd, yyyy");
try this:
DateTime dt = DateTime.ParseExact(dateString, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
You can use ParseExact or better TryParseExact (that not generate exception when fail), with this two method you can pass the format provider description as parameter :
DateTime.ParseExact(strDate, "dd/MM/yyyy HH:mm:ss", null);
You need DateTime.ParseExact
You should use Convert.ToDateTime(String)
http://msdn.microsoft.com/es-es/library/xhz1w05e(v=VS.90).aspx
You'll have to see if Convert.ToDateTime accept the format you want to input it.
your date format is mm/dd/yyyy there is no month of 18
try this
DateTime.Parse("04/18/2011 4:57:20PM").ToShortDateString();
Have you tried this
DateTime.Now.ToString("MMM dd, yyyy")
精彩评论