1

I have a string that I get from a source that represents a date and comes like this "19941201" (Year+Month+Day).

I need it to be "01/12/1994" or even "01-12-1994".

I am retrieving this data from a List using linq.

Is there a 'neat' way to do this? I use c# and .Net4.0 !

Marijn
  • 10,367
  • 5
  • 59
  • 80
Jenninha
  • 1,357
  • 2
  • 20
  • 42

4 Answers4

6
DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);

Then you can simply do datetime.ToString("dd/MM/yyyy");

paulslater19
  • 5,869
  • 1
  • 28
  • 25
  • 1
    Thanks, this works for what I need! Just to complete the answer `CultureInfo` is imported from 'System.Globalization'. – Jenninha Jul 17 '12 at 11:31
2

Have a look at this Here

and read this Here

You will get a better understanding when you read up on rather than someone else doing it for you. I learnt the hard way

Inkey
  • 2,189
  • 9
  • 39
  • 64
1
    static void Main(string[] args)
    {
        Console.WriteLine(ParseDate("19941201"));
        Console.ReadLine();
    }

    public static string ParseDate(string uglyDate)
    {
        return DateTime.ParseExact(uglyDate, "yyyyMMdd", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
    }
NPSF3000
  • 2,421
  • 15
  • 20
0

Have a look at the DateTime.Parse() and DateTime.ToString() methods: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

Zak
  • 724
  • 4
  • 18