You must Sign In to post a response.
  • Category: ASP.NET

    How To Convert a Date value to DateTime Format in Asp.Net C#

    Hi Developers,

    I want to Change Some Date Cell backcolour. My Requirement is following
    Admin Side (Doctor) :
    1. Select and insert Available date.
    2.The Admin Selected date is to be Stored on the database.

    User Side (Patient) :
    The user Can be able to Doctor's Available time.
    The Available(Admin Selected Dates) Date Box only Change to Green or any Colour.
    inside the Calendar.

    I am try to done the Task. When i am run my Code i got the following Error.

    Server Error in '/mY pROJECT 04-04-2016' Application.
    String was not recognized as a valid DateTime.

    Line 232: for (int i = 0; i < dt.Rows.Count; i++)
    Line 233: {
    Line 234: occasionDate = Convert.ToDateTime(dt.Rows[i]["appointmentdate"].ToString());
    Line 235: if (e.Day.Date == occasionDate)
    Line 236: {


    My C# Code is am Added Below

    protected void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    DataTable dt = new DataTable();
    string str = "SELECT * FROM tbl_OnlineChatCalendar";
    OleDbCommand com = new OleDbCommand(str, con);
    OleDbDataAdapter sqlda = new OleDbDataAdapter(com);
    dt = new DataTable();
    DateTime occasionDate;
    string occasionType = string.Empty;
    if (dt.Rows.Count > 0)
    for (int i = 0; i < dt.Rows.Count; i++)
    occasionDate = Convert.ToDateTime(dt.Rows[i]["appointmentdate"].ToString());
    if (e.Day.Date == occasionDate)

    e.Cell.BackColor = System.Drawing.Color.Green;
    e.Cell.BackColor = System.Drawing.Color.Red;


    i am struggling and tried a lot of types. but i cant able to resolve the error.
    So Anyone of you Please help me to i am Resolve the Error.

    Thanking You
  • #765383
    I recommend to use given code snippet to Convert a string value to DateTime Format in Asp.Net C# by using using String.Format method
     String.Format("{0:d/M/yyyy HH:mm:ss}", dt); 
    String.Format("{0:d/M/yyyy HH:mm:ss}"
    String.Format("{0:t}", dt);
    String.Format("{0:d}", dt);
    String.Format("{0:T}", dt0;

    Few for useful URL as given below

  • #765386
    Hi Paul,

    The cause of this "String was not recognized as a valid DateTime" error is, if you are trying to convert the string into datetime but the string is not supported / not valid format that time you got this type of error.

    To resolve this error, request you to kept breakpoint and debug the code and check the line no 234;

    occasionDate = Convert.ToDateTime(dt.Rows[i]["appointmentdate"].ToString());

    as per my point of view the appointmentdate is not given the valid datetime format, so that only while convert that into datetime it's throwing the "String was not recognized as a valid datetime" error.

    Hope this will helpful to you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #765400
    Do not directly go for 'Convert.ToDateTime' it may throw 'Invalid Date time format' issue better to parse it first and then compare it with other date
    see below snippet
    string dt = "24/12/2010";
    DateTime dt1;
    //always use InvariantCulture for dateformat provider as it support large number of customized datetime formats
    System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
    dt1 = DateTime.ParseExact(dt, "dd/MM/yyyy", provider);
    MessageBox.Show("String successfully converted to DateTime" + dt1.Year);

    you have code it in 'DayRender' event which Occurs when each day is created in the control hierarchy for the Calendar control, does it right ? when you want to fire this event ?

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765401
    Thanks mr.Prasad and Thanks Mr.Naveen Your point of view is extremely correct.
    Thanks a lot Naveen.Now my code is running.
    Thanking you

  • #765405

    Refer below..

    to convert to DateTime format..
    DateTime date = DateTime.ParseExact(textboxDate.Text, "dd/MM/yyyy", null);

    Hope this will help you

    Never lose hope..You never know what tomorrow will bring

  • Sign In to post your comments