I have date in string format 20/05/2016
:
string weekEndDate="20/05/2016"
when convert it to DateTime,An error Occured:
DateTime EndDate = Convert.ToDateTime(weekEndDate);
String was not recognized as a valid DateTime.
I have date in string format 20/05/2016
:
string weekEndDate="20/05/2016"
when convert it to DateTime,An error Occured:
DateTime EndDate = Convert.ToDateTime(weekEndDate);
String was not recognized as a valid DateTime.
You can use ParseExact
method to parse the string into DateTime
.
string weekEndDate = "20/05/2016";
DateTime EndDate = DateTime.ParseExact(weekEndDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or you can use TryParseExact
it will not throw exception
if string
is not parsed into DateTime
DateTime.TryParseExact(weekEndDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out EndDate);
Use TryParseExact
string d1 = "11/18/2016 11:45:44 AM";
string d2 = "11/18/2016 11:45:59 AM";
DateTime frmdate;
DateTime todate;
CultureInfo enUS = new CultureInfo("en-US");
bool f = DateTime.TryParseExact(d1, "M/dd/yyyy HH:mm:ss tt", enUS, DateTimeStyles.None, out frmdate);
bool t = DateTime.TryParseExact(d2, "M/dd/yyyy HH:mm:ss tt", enUS, DateTimeStyles.None, out todate);
TimeSpan val = frmdate - todate;