Date Format

  • Thread starter Thread starter Tom Pair
  • Start date Start date
T

Tom Pair

Hi,

I use DateTime.Now to display the current date and time. For instance,
it's default format is 2003-10-29 21:23:36.

How can I change the format as follows?

10/29/2003 9:23:36 PM

MM/DD/YY HH/MM/SS AM/PM

Thanks for help

Tom
 
Tom,

When using the ToString method on the DateTime structure, you can use
the following format string to get the format that you wish:

M/D/yyyy h:m:s tt

If you want to have a leading zero for any of the arguments (month, day,
hour, minute, second), then double up the format character for that section.

Hope this helps.
 
Hi,

I replaced DateTime.Now with DateTime.Now.ToString(). But,
the date is same as before.

Could you teach me how to get the M/D/yyyy h:m:s tt format
from that date string?

Thanks for help

Tom
 
Tom,

I don't understand what you are trying to do. The representation of a
DateTime structure in .NET is just that, an instance of the DateTime
structure.

If you want to get that date in a string that is formatted that way,
take the string that I posted, and then pass it into the ToString method,
like this:

// Get the current date/time in the string format we desire.
string pstrFormattedDateTime = DateTime.Now.ToString("M/D/yyyy h:m:s tt");
 
I tried to use DateTime.Now.ToString("M/D/yyyy h:m:s tt")

The time is correct. But, the date prints 10-D-2003.

If I use DateTime.Now.ToString("m/d/yyyy h:m:s tt")

The date prints 39-10-2003.

If I use DateTime.Now.ToString("M/d/yyyy h:m:s tt")

The date prints 10-10-2003.

How can the date print 10/10/2003? or, it is no way to do it?

Thanks for your help.
 
How can the date print 10/10/2003? or, it is no way to do it?

Use this:

dd/MM/yyyy

Use the "exact" spelling, including upper and lower case.

/m
 
Back
Top