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 is expired now");
}
else
{
Response.Redirect("Default.aspx", false);
}
}
and I'm getting error like:
String was not recognized as a valid DateTime.
Try with DateTime.ParseExact()
method;
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
DateTime date = DateTime.ParseExact(myDate, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Here is a DEMO
.
You can check more Custom Date formats from Custom Date and Time Format Strings
since you have this date formatted like 09/04/2013 10:41:45 AM
DateTime dt1 = DateTime.ParseExact(myDate, "MM/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture)
if 09
is day, change the pattern into dd/MM/yyyy hh:mm:ss tt
For more information about Date and Time Format Strings,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.