tags:

views:

1476

answers:

3

hi guys, I used the following code. string result = DateTime.ParseExact("24/5/2009 3:40:00 AM", "d/M/yyyy h:mm", CultureInfo.InvariantCulture) .ToString("M/d/yyyy h:mm");

In my textbox time after 12'0 Clock( for eg:24/5/2009 14:00 ) i want to convert to format as 24/5/2009 2:00 PM.Can anybody give appropriate code?

+4  A: 

Assuming your original date starts as a string and you want it to end up as another string, you could do something like this in C#:

string from = "24/5/2009 3:40:00 AM";
DateTime dt = DateTime.ParseExact(from, "d/M/yyyy h:mm:ss tt", , System.Globalization.CultureInfo.InvariantCulture);
string to = dt.ToString("MM/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

*Edit: Fixed the formatting strings. It assumes your original strings only have single digit day, month, and hour values when they are each less than 10. It also assumes you want them all to be 2 digit values for the result.

Here's the MSDN Documentation on the subject: Custom Date and Time Format Strings

Ryan
You will want to add System.Globalization.CultureInfo.InvariantCulture as the second parameter of the dt.ToString call in order to ensure that the formatted string does indeed use '/' characters:string to = dt.ToString("MM/dd/yyyy hh:mm:ff tt", System.Globalization.CultureInfo.InvariantCulture);
Fredrik Mörk
Good catch, done.
Ryan
The formatting string is wrong. "ff" is not seconds, it's seconds fraction. You need to use "ss" to get the seconds.
Guffa
Changed ff to ss
Patrick McDonald
A: 
Dim result As String = DateTime.ParseExact("24/5/2009 3:40:00 AM",
                                    "d/M/yyyy h:mm:ss tt",
                                     CultureInfo.InvariantCulture)
                               .ToString("M/d/yyyy h:mm:ss tt")

See also:

CMS
That's not VB code...
Guffa
Changed variable declaration to VB syntax...
CMS
A: 

A DateTime value doesn't have a format at all, so if the source is a datetime value it's not actually a conversion but a just matter of formatting the output.

You can use the custom foratting string "M'/'d'/'yyyy h':'mm':'ss tt" to get exactly that format. (Your question mentions two different formats, so I assume that it's the one represented by the example data that you want; the one that has seconds.)

Example:

Dim result As String = source.ToString("M'/'d'/'yyyy h':'mm':'ss tt")

If the source is a string so that it's actually a conversion that you need, you can either use string operations to chop up the string into components and rearrange them, or you can parse the string into a DateTime value and reformat it into a new string.

Example:

Dim source As String = "24/5/2009 3:40:00 AM"
Dim d As DateTime = DateTime.ParseExact(source, "d'/'M'/'yyyy h':'mm':'ss tt", CultureInfo.InvariantCulture)
Dim result As String = d.ToString("M'/'d'/'yyyy h':'mm':'ss tt")

Or:

Dim source As String = "24/5/2009 3:40:00 AM"
Dim d As String() = source.Split("/".ToCharArray(), 3)
string result = d(1) + "/" + d(0) + "/" + d(2)
Guffa