How can i convert String like 20100102 into datetime in a formate of dd/MM/yyyy?
A:
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
2010-01-01 19:24:13
Ashish
2010-01-01 19:26:42
Bah, this really isn't the way to do it.
Noldorin
2010-01-01 19:27:46
Custom date parsing FTL. Don't reinvent the wheel.
Aaronaught
2010-01-01 19:30:07
Indeed, this isn't the right way to do it. This will give somewhat cryptic error messages if the format isn't quite right, or if the string is too short.
Jon Skeet
2010-01-01 19:54:28
This is only used whene our strStartDateMain will comes in a same range and length.
Ashish
2010-01-01 20:06:17
he is just tried. for own question. so dont give him minus point. just leave comment. and tell him better way.
Sikender
2010-01-04 18:25:21
+8
A:
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
2010-01-01 19:25:37
Right - except that it probably makes sense either to use `CultureInfo.CurrentCulture` or `CultureInfo.InvariantCulture` dependening on the scenario.
Noldorin
2010-01-01 19:26:52
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
2010-01-01 19:32:18
+8
A:
var result = DateTime.ParseExact("20100102", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Modify as needed.
helium
2010-01-01 19:26:28
+6
A:
var userdateformat = DateTime.ParseExact("20101020", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
Modify as you want to modify.
Sikender
2010-01-01 19:46:43