Can someone help me convert the string 14/04/2010 10:14:49.PM to datetime in C#.net without losing the time format?
var date = DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss.tt", null);
For string representation use
date.ToString(@"dd/MM/yyyy hh:mm:ss.tt");
Also you can create extention method like this:
public enum MyDateFormats
{
FirstFormat,
SecondFormat
}
public static string GetFormattedDate(this DateTime date, MyDateFormats format)
{
string result = String.Empty;
switch(format)
{
case MyDateFormats.FirstFormat:
result = date.ToString("dd/MM/yyyy hh:mm:ss.tt");
break;
case MyDateFormats.SecondFormat:
result = date.ToString("dd/MM/yyyy");
break;
}
return result;
}
DateTime result =DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy HH:mm:ss.tt",null);
You can now see the PM or AM and null value for format provider
DateTime.ParseExact(@"14/04/2010 10:14:49.PM", @"dd/MM/yyyy hh:mm:ss");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With