5

How can i convert String like 20100102 into datetime in a formate of dd/MM/yyyy?

Jim Anderson
  • 3,602
  • 2
  • 24
  • 21
Ashish
  • 183
  • 3
  • 4
  • 12

5 Answers5

14
var userdateformat = DateTime.ParseExact("20101020", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as you want to modify.

sikender
  • 5,883
  • 7
  • 42
  • 80
9
IFormatProvider culture = new CultureInfo("en-EN", false); // use your culture info
DateTime dt = DateTime.ParseExact(myDateTimeString, "yyyyMMdd", culture, DateTimeStyles.NoCurrentDateDefault); 

yyyyMMdd is input format here.

And then if you wish convert it to string:

String output = String.Format("{0:dd/MM/yyyy}", dt);
JCasso
  • 5,423
  • 2
  • 28
  • 42
  • Right - except that it probably makes sense either to use `CultureInfo.CurrentCulture` or `CultureInfo.InvariantCulture` dependening on the scenario. – Noldorin Jan 01 '10 at 19:26
  • I changed user override to false. So it uses default now. But it will not cause any problems here even it overrides. Or am I wrong? – JCasso Jan 01 '10 at 19:32
9
var result = DateTime.ParseExact("20100102", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as needed.

helium
  • 1,077
  • 1
  • 9
  • 15
4

You can use DateTime.ParseExact method

Giorgi
  • 30,270
  • 13
  • 89
  • 125
-3
string strStartDateMain = "20100102";
string strStartDateFinal = new DateTime(Convert.ToInt32strStartDateMain.Substring(0, 4)), Convert.ToInt32(strStartDateMain.Substring(4, 2)), Convert.ToInt32(strStartDateMain.Substring(6))).ToString("dd/MM/yyyy");
Ashish
  • 183
  • 3
  • 4
  • 12