Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hey,
I got this error on running my project "String was not recognized as a valid DateTime." In my project I have passed a parameter string AddedDate and it is stored as:
objmyprojBO.AddedDate = Convert.ToDateTime(AddedDate);
and found the above error.
Please help me.

[Edit - CHill60 - OP comment from "solution"]
The problem is the format of date. I have used a date picker function to get calender in text box, by default the date format is MM/dd/yyyy. But ASP code uses dd/MM/yyyy format. So there is an error above occured. So I need to specify the correct format of date as shown below.
$("#txtAddedDate").datepicker({dateformat : "dd/mm/yyyy"});.
Posted
Updated 14-May-14 2:34am
v2
Comments
[no name] 13-May-14 7:30am    
Make sure that "AddedDate" is a valid date time.
My Doubt 13-May-14 7:58am    
Yea its valid date time. Its my jquery :
var AddedDate = $('#txtAddedDate').val();
but in here and the date format will be : MM/dd/yyyy, but here:
objmyprojBO.AddedDate = Convert.ToDateTime(AddedDate);
dete format is: dd/MM/yyyy

1. First, check that the date string is a valid one using tryparse;
2. Specify the desired date format;
3. then can do the conversion.

using System;
 
public class Program
{
    public static void Main()
    {
          // change 2/22/2014 5:08:46 PM to 2014-02-22.
          string dateString = "2/22/2014 5:08:46 PM";
 
          Console.WriteLine("The original string is {0}", dateString);
 
          DateTime dt;
 
          if (DateTime.TryParse(dateString, out dt))
          {
               string format = "yyyy-MM-dd";
               Console.WriteLine(dt.ToString(format));
          }
          else
          {
              Console.WriteLine("Not a valid datetime");
          }
    }
}
 
Share this answer
 
CultureInfo culture = new CultureInfo( cultureName );
Convert.ToDateTime(AddedDate,culture);
check the Iformat of the passing string parameter,
 
Share this answer
 
The problem is the format of date. I have used a date picker function to get calender in text box, by default the date format is MM/dd/yyyy. But ASP code uses dd/MM/yyyy format. So there is an error above occured. So I need to specify the correct format of date as shown below.
$("#txtAddedDate").datepicker({dateformat : "dd/mm/yyyy"});.
 
Share this answer
 
Comments
CHill60 14-May-14 8:33am    
Don't post solutions to your own question. If you need to add something to your post then use the "Improve question" link. If you want to respond to a particular poster then use the Reply button next to their comment or the "Have a Question or Comment?" link next to their solution
Make sure entered string is in proper format . Then do the following steps .

CultureInfo culture = new CultureInfo(cultureName);
Convert.ToDateTime(AddedDate,culture);
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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