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

    Question

  • 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
    Monday, November 13, 2006 9:14 PM

Answers

  • use this code:

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

    Tuesday, November 14, 2006 8:55 AM
  • 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...

    Monday, November 13, 2006 9:22 PM

All replies

  • If I copy your code into a progam and run it I get "20061113".  Are you sure that's all the code?
    • Proposed as answer by KForem Monday, January 21, 2013 12:23 PM
    Monday, November 13, 2006 9:21 PM
  • 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...

    Monday, November 13, 2006 9:22 PM
  • 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

    Monday, November 13, 2006 9:27 PM
  • 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

    Tuesday, November 14, 2006 4:11 AM
  • use this code:

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

    Tuesday, November 14, 2006 8:55 AM
  • Thanks Khalid Ashraf 

    It works

    Tuesday, November 14, 2006 9:16 AM
  • Hi guys 

    My question is how to convert date in string format "20110621" to DateTime format???

    summee


    Tuesday, June 21, 2011 7:33 AM
  • Hi there,

     

    what about using like that "DateTime.Now.ToString("yyyyMMdd00000000");" and do SubString.

     


    Mehmet Metin Altuntas
    Thursday, August 04, 2011 4:28 AM