Changing a DateTime format --- please help!

A

almurph

Hi,

Hope that you can help me with this. I'm calling DateTime.Now and
storing in a DateTime container. I see that the pattern format is: m/
dd/yyyy

I need it to be in the format: yyyy-MM-dd HH:MM:SS as type DateTime
but I don't know how to do this! Can anyone help me please? Any
comments/advice/suggestions/code-sampels much appreciate.

Cheers,
Al.
 
N

Nicholas Paldino [.NET/C# MVP]

Al,

If you have a DateTime type, then the value is going to be consistent no
matter what. What you are looking at is the ^representation^ of the
DateTime in string form, which can vary tremendously based on culture,
preferences, etc, etc. However, when you look at it in the debugger, that
is just a representation, not actually how it stores the DateTime
internally.

If you need the DateTime represented in a certain format, just call the
ToString method on it, passing the format that you need it to be represented
in.
 
B

Bjørn Brox

(e-mail address removed) skrev:
Hi,

Hope that you can help me with this. I'm calling DateTime.Now and
storing in a DateTime container. I see that the pattern format is: m/
dd/yyyy

I need it to be in the format: yyyy-MM-dd HH:MM:SS as type DateTime
but I don't know how to do this! Can anyone help me please? Any
comments/advice/suggestions/code-sampels much appreciate.
Define DateTime container?

What you see is not the stored value of a DateTime class, but the way it
is displayed for you, probably by some default ToString() pattern which
may vary depending on culture, language etc.

If you want to store a DateTime value as a specific string format you
can control it by using your own argument for ToString():

String datetime_as_string = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
 
B

BlackWasp

As others have said, if you are converting to a string representation of the
date, use ToString(). There is a whole heap to think about here though
including globalisation (if the application is not for a particular
local-region-only solution).

I have put together an article on DateTime manipulation that includes
information on standard formats and "Picture" formatting at
http://www.blackwasp.co.uk/CSharpDateManipulation.aspx.
 
R

Rad [Visual C# MVP]

Hi,

Hope that you can help me with this. I'm calling DateTime.Now and
storing in a DateTime container. I see that the pattern format is: m/
dd/yyyy

I need it to be in the format: yyyy-MM-dd HH:MM:SS as type DateTime
but I don't know how to do this! Can anyone help me please? Any
comments/advice/suggestions/code-sampels much appreciate.

Cheers,
Al.

As has been suggested, one way is to use ToString() with the appropriate
format specifiers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top