Hi All,

I want to convert any (current datetime differs in format depending regional setting) into 20090622172120 format.
How do I do that? I want solution that work with any regional setting.

many thanks,

Jeet

Recommended Answers

All 4 Replies

Just call .ToString() with the string formatting options you want. I'm guessing you want year.month.day.hour.minute.sec? Use:
I have separated the first code line with .'s for readability.

DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss");
      DateTime.Now.ToString("yyyyMMddHHmmss");

Just call .ToString() with the string formatting options you want. I'm guessing you want year.month.day.hour.minute.sec? Use:
I have separated the first code line with .'s for readability.

DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss");
      DateTime.Now.ToString("yyyyMMddHHmmss");

thanks friend, this is solved, how do I get the original string back?
Means converting back year.month.day.hour.minute.sec to the system default datetime... I am sorry for throwing second question later.

thanks
Jeet

You could always mark this thread as solved and create a new thread ;)

I'm sure there is another way to do this using format providers or such but I usually create the date myself:

string sDateTime = DateTime.Now.ToString("yyyy.MM.dd.HH.mm.ss");
      string[] parts = sDateTime.Split(new char[] { '.' }, StringSplitOptions.None);
      DateTime reconstructed = new DateTime(
        Convert.ToInt32(parts[0]), //year 
        Convert.ToInt32(parts[1]), //month
        Convert.ToInt32(parts[2]), //day
        Convert.ToInt32(parts[3]), //hr
        Convert.ToInt32(parts[4]), //min
        Convert.ToInt32(parts[5])); //sec

Change this over to .Substring() calls to get the relevant date information if you're not using the dotted date format

many thanks!

Jeet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.