I'm trying to convert format of a DateTime on my asp.net mvc web app.
I've this code :
date = Request["date"].AsDateTime().ToString("MM-dd-yyyy HH:mm:ss");
That code works perfectly when the Request["date"] as only one digit for the day
(like 08-03-2016) but when the date hava two digits date
( like 15-03-2016 ),
it returns 01-01-0001... Can someone explains me why and tell me how to makeit better ?
Thanks in advance !
EDIT :
More of code I'm using :
Javascript date picker for the date selection:
<script>
$(document).ready(function() {
$('.date').datepicker({ dateFormat: "dd/mm/yy" });
});
</script>
Code for format :
String date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 0, 0, 0).ToString("dd/MM/yyyy HH:mm:ss");
string[] formats = { "dd-MM-yyyy" };
DateTime resultDate = new DateTime();
if (DateTime.TryParseExact(Request["date"], formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out resultDate))
{
//if everything good you will have your date in resultDate variable
date = resultDate.ToString("MM-dd-yyyy HH:mm:ss");
}
.AsDateTime()extension method is parsing the data using a culture which isMM-dd-yyyy- does11-03-2016work?