c# - String was not recognized as a valid DateTime -
my code is:
protected void page_load(object sender, eventargs e) { string mydate = request.querystring["period"]; if (!string.isnullorempty(mydate)) { mydate = mydate.replace("!", ":"); } datetime dt1 = convert.todatetime(mydate); datetime dt2 = datetime.now; timespan variable = dt2 - dt1; if (variable.totalminutes > 5) { response.write("download time expired now"); } else { response.redirect("default.aspx", false); } }
and i'm getting error like:
string not recognized valid datetime.
try datetime.parseexact()
method;
converts specified string representation of date , time datetime equivalent using specified format , culture-specific format information. the format of string representation must match specified format exactly.
datetime date = datetime.parseexact(mydate, "dd/mm/yyyy hh:mm:ss tt", cultureinfo.invariantculture);
here demo
.
you can check more custom date formats custom date , time format strings
Comments
Post a Comment