Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
string d = Request.Form["StartDate"];
DateTime dt = Convert.ToDateTime(d);
 string e = Request.Form["EndDate"];
 DateTime ed = Convert.ToDateTime(e);
objCoupons.EndDate = ed;
objCoupons.StartDate = dt;


this is my code .
the value of d is in format dd/mm/yyyy eg. 10/10/2017.
while converting to date time it throws an error that
when converting strig to datetime parse the string to take date before putting each variable to datetime object.

What I have tried:

i have tried using datetime parse
or another convert thing and invariant culture etc etc
Posted
Updated 17-Oct-17 22:27pm
v2

use DateTime.TryParseExact Method (System)[^]

string strDate = "10/10/2017";
         DateTime dtTarget = default(DateTime); // (or) DateTime.MinValue , use this for comparision
         if (DateTime.TryParseExact(strDate, "dd/mm/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out dtTarget))
         {
             // use the  dtTarget object to get the parsed dates
         }
         else {
             // show the error message
         }
 
Share this answer
 
v2
If you know that the string is always formatted in a specific format use the DateTime.ParseExact Method (String, String, IFormatProvider) (System)[^] or the DateTime.TryParseExact method.

If you know the culture settings that has been used when creating the string, use one the DateTime.Parse Method (System)[^] or the corresponding TryParse methods that accepts a culture info parameter.

In all other cases it not possible to convert strings to dates.

To avoid date conversion problems store them in binary format whenever possible or use a defined format (fixed or stored/passed as additional argument).
 
Share this answer
 
v2
Comments
Thanks7872 18-Oct-17 4:29am    
+5. Should get accepted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900