Format required
string format = "dd/M/yyyy";
I don’t understand anything, why split the string into concatenation, so how do you get the same?
If the entrance is 12/4/2012, after splitting into '/', you will get 12, 4, 2012 and then join them back to get "12/4/2012". Why is this?
Also, if you really need this split, you can save it to an array, so you don't need to split it 3 times:
var splits = lbl_TransDate.Text.Split('/'); DateTime.ParseExact(splits[0] + "/" + splits[1] + "/" + splits[2], ...);
If you do not trust the input, the array of sections may not be long = 3, and moreover, you can use DateTime.TryParseExact
EDIT You can use overload with multiple formats. So if the input can be 12/4/2012 or 12/04/2012, you can provide both formats
var formats = new[] {"dd/M/yyyy","dd/MM/yyyy"}; var date = DateTime.ParseExact("12/4/2012", formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal);
source share