We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Date format

Nugs
Nugs asked
on
Medium Priority
246 Views
Last Modified: 2010-04-15
I have a date and time that is returned to me as:

1998-06-22T13:00

I need to convert this into

1998-06-22

Is there a easier way besides substring it?

Nugs
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
Try this:

            string dt = "1998-06-22T13:00";
            DateTime d = DateTime.Parse(dt);

            string formatted = d.ToShortDateString();

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Or, depending on your locale you may need to use a custom format:

            string dt = "1998-06-22T13:00";
            DateTime d = DateTime.Parse(dt);

            string formatted = d.ToString("yyyy-MM-dd");

Author

Commented:
I have been trying ToString("yyyy-MM-dd"); but keep getting a ToString only takes 1 argument error... ???
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Can you show the code in context ?
billycotxTeam Lead

Commented:
Nugs,

Try this.
string dt = "1998-06-22T13:00";
DateTime thisDate = Convert.ToDateTime(dt);
string strDate = thisDate.ToString("yyyy-MM-dd");
icr

Commented:
I would advice against using Convert when you want to Parse. Parse when you are doing just that, parsing strings, means you can use a consistent convention even with new custom types (you can't add Convert.ToMyOwnType() yet).

Are you sure you've copied the line or error correctly? Are you sure you are using ToString("yyyy-MM-dd"); on a DateTime?
billycotxTeam Lead
Commented:
icr,

That tested that code.
ToString on a string field only offer two options:
billycotxTeam Lead

Commented:
icr,

I fat fingered the previous post sorry about that.

ToString on a string field only offer two options:
     ToString() and ToString(IFormatProvider)

ToString on a date field offers four options (overloads)
    ToString(), ToString(IFormatProvider), ToString(string format) and ToString(string format, iFormatProvider)



   
icr

Commented:
Sorry billycotx, I don't understand what your getting at.
billycotxTeam Lead

Commented:
Nugs,

icr has a point about Convert and Parse.

Here is a link to info on DataTime.ParaseExact()
http://authors.aspalliance.com/aspxtreme/sys/DateTimeClassParseExact.aspx

Hope this helps.
billycotxTeam Lead
Commented:
icr,
I was just initially responding to Nugs comment of the following:

I have been trying ToString("yyyy-MM-dd"); but keep getting a ToString only takes 1 argument error... ???

Since Nugs received this error I assumed he was working with a string variable. ToString("yyyy-MM-dd"); on a datetime should not have given "only takes 1 argument error".

That's what I was pointing out.
billycotxTeam Lead

Commented:
Nugs,

The following worked for me.

string dt = "1998-06-22T13:00:00";
string newdate = DateTime.ParseExact(dt, "yyyy-MM-ddTHH:mm:ss",ifProvider).ToString("yyyy/MM/dd");

Hope this helps.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.