Datetime string error in C #

Suppose I have the following code to convert datetime to string:

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

      

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

How to implement it?

+2


source to share


4 answers


use this:

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

      



Below are all the available options to convert DateTime to string

+13


source


According to the DateTime.ToString documentation, the characters you need to add is t, so this should work:

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

      



One won't give you "P" or "A" and two will give you "PM" or "AM".

Please note that depending on the current CultureInfo, you may or may not receive AM / PM.

+5


source


you must use lowercase "t" ...

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

      

+1


source


DateTime dt;

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

      

0


source







All Articles