views:

100

answers:

3

Suppose I have following code to convert datetime to string:

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mm")

If the dt is 15/02/2009 08:22, I want to the string is 15/02/2009 08:22AM If the dt is 15/02/2009 20:22, I want to the string is 15/02/2009 08:22PM

How to implement it?

+13  A: 

use this:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

Here are all of the available options for converting DateTime to string

scottm
+5  A: 

As per the documentation of DateTime.ToString, the characters you need to add are t's, so this should work:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

One 't' would give you 'P' or 'A', and two will give you 'PM' or 'AM'.

Note that depending on your current CultureInfo, you might, or might not, get the AM/PM.

Lasse V. Karlsen
+1  A: 

you should use lowercase "t"...

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mmtt")
Eclipsed4utoo