7 Replies - 290 Views - Last Post: 18 June 2013 - 01:28 PM Rate Topic: -----

#1 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Converting to a Date

Posted 18 June 2013 - 04:58 AM

So I have a string with a value of "18JUN2013" and want to convert that to "18 June 2013"
This needs to be dynamic, so it should work with each month.

I did a quick Google but couldn't come up with anything (It's one of those days).

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Converting to a Date

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Converting to a Date

Posted 18 June 2013 - 05:03 AM

Did you even try using String.Substring() to take the string apart into component pieces? Or you could use a Regex to take the string apart.
Was This Post Helpful? 0
  • +
  • -

#3 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Re: Converting to a Date

Posted 18 June 2013 - 05:10 AM

Yes, of course, but I don't know how to convert "AUG" into "AUGUST" (And such).
I believe I may have to do something with IFormatProvider
 IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);

And then use that in a DateTime Parse, but I'm really not sure.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,521
  • Joined: 08-April 09

Re: Converting to a Date

Posted 18 June 2013 - 05:29 AM

And by dynamic you mean culture sensitive?

Have you read Standard Date and Time Format Strings first and Custom Date and Time Format Strings

This post has been edited by Michael26: 18 June 2013 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Converting to a Date

Posted 18 June 2013 - 05:30 AM

See DateTime.TryParseExact(): http://msdn.microsof...y/ms131044.aspx
Was This Post Helpful? 1
  • +
  • -

#6 oyyou  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 184
  • Joined: 26-April 10

Re: Converting to a Date

Posted 18 June 2013 - 05:56 AM

So it was actually quite simple. I don't know how I didn't know this already
        public static string FormatDate(string input)
        {
            DateTime dt;
            var rawr = DateTime.TryParse(input, out dt);

            string something = dt.ToString("dd MMMM yyyy");

            return something;
        }


Was This Post Helpful? 1
  • +
  • -

#7 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: Converting to a Date

Posted 18 June 2013 - 06:09 AM

View Postoyyou, on 18 June 2013 - 04:58 AM, said:

So I have a string with a value of "18JUN2013" and want to convert that to "18 June 2013"
This needs to be dynamic, so it should work with each month.


If all you need is converting from this specific format to that specific format, you could use LINQ.
Make a conversion function that:

- extracts the day by applying TakeWhile char.IsDigit
- extracts the month by applying Skip with the count of day digits and TakeWhile char.IsLetter, then converts the string as you like in a simple each-month-case switch
- gets the year by skipping the sum of the first 2 counts
- returns a StringBuilder that concatenates them back

One little problem is that those functions return IEnumerable<char>, so you should convert the results back to string when needed.

PS: Or stop reading and do what you did.

This post has been edited by coder3788: 18 June 2013 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3359
  • View blog
  • Posts: 11,382
  • Joined: 12-December 12

Re: Converting to a Date

Posted 18 June 2013 - 01:28 PM

Quote

If all you need is converting from this specific format to that specific format, you could use LINQ. Make a conversion function that ..

Wow! (I stopped reading at that point ;) )

Skydiver is on the mark: TryParseExact is designed specifically for this task.

Quote

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.

You might then use ToString() (or similar) to obtain the new string version.

But if TryParse works then there seems no reason to change.

This post has been edited by andrewsw: 18 June 2013 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1