Date format

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
LVL 2
NugsAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Try this:

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

            string formatted = d.ToShortDateString();
0
 
Carl TawnSystems and Integration DeveloperCommented:
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");
0
 
NugsAuthor Commented:
I have been trying ToString("yyyy-MM-dd"); but keep getting a ToString only takes 1 argument error... ???
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Carl TawnSystems and Integration DeveloperCommented:
Can you show the code in context ?
0
 
billycotxCommented:
Nugs,

Try this.
string dt = "1998-06-22T13:00";
DateTime thisDate = Convert.ToDateTime(dt);
string strDate = thisDate.ToString("yyyy-MM-dd");
0
 
icrCommented:
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?
0
 
billycotxConnect With a Mentor Commented:
icr,

That tested that code.
ToString on a string field only offer two options:
0
 
billycotxCommented:
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)



   
0
 
icrCommented:
Sorry billycotx, I don't understand what your getting at.
0
 
billycotxCommented:
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.
0
 
billycotxConnect With a Mentor 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.
0
 
billycotxCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.