How to Convert Current Date into string "yyyyMMdd" like "20061113"

hi

Is there any way to convert the Current Date into string "yyyyMMdd" like "20061113"

I have tried this but it did not work

DateTime theDate = DateTime.Now;
theDate.ToString("yyyyMMdd");

It gives me 2006/11/13 10:50PM

But i want a string and not a Date like this "20061113"

Thanks


Answer this question

How to Convert Current Date into string "yyyyMMdd" like "20061113"

  • Jon Limjap

    Thanks Khalid Ashraf

    It works


  • steal

    Thanks to you all

    My problem was with user's locale settings. I have tried the code in different PC and it works perfectly.


    How can I specify the Globalization Culture to en-US (only in geting this date not the hole application)

    Kind Regards


  • Martin Bayly

    use this code:

    MessageBox.Show(DateTime.Now.ToString("yyyyMMdd", System.Globalization.CultureInfo.GetCultureInfo("en-US")));



  • johngarcia

    hi,

    as what peter said you code working fine and all those lines working give a result close to what you want

    static void Main(string[] args)

    {

    DateTime dt = DateTime.Now;

    Console.WriteLine(dt.ToString("yyMMMMdd"));

    Console.WriteLine(dt.ToString("yyyMMMMdd"));

    Console.WriteLine(dt.ToString("yyyMMMdd"));

    Console.WriteLine(dt.ToString("yyyMMdd"));

    }

    hope this helps



  • robinjam

    If I copy your code into a progam and run it I get "20061113".  Are you sure that's all the code

  • kidwidahair

    Also, if you want specific number of digits, you should use

    .ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

    To ensure the user's locale settings don't affect it...



  • How to Convert Current Date into string "yyyyMMdd" like "20061113"