• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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
0
Nugs
Asked:
Nugs
  • 6
  • 3
  • 2
  • +1
3 Solutions
 
Carl TawnSystems 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
billycotxCommented:
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
 
billycotxCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now