Share via


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

Question

Monday, November 13, 2006 9:14 PM

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

All replies (8)

Monday, November 13, 2006 9:22 PM ✅Answered | 1 vote

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...


Tuesday, November 14, 2006 8:55 AM ✅Answered | 1 vote

use this code:

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


Monday, November 13, 2006 9:21 PM

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


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


Tuesday, November 14, 2006 4:11 AM

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 9:16 AM

Thanks Khalid Ashraf 

It works


Tuesday, June 21, 2011 7:33 AM

Hi guys 

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

summee


Thursday, August 4, 2011 4:28 AM

Hi there,

 

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

 

Mehmet Metin Altuntas