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

    How to change the textbox date to ddmmyyy format in c#

    i want to validate the textboxdate and gridview row date in c#

    My code as follows

    DateTime selectedfromdate = Convert.ToDateTime(Txtfromdate.SelectedDateValue);
    for (int i = 0; i < GvSch.Rows.Count; i++)
    {
    if (selectedfromdate == Convert.ToDateTime(rows.Cells[0].Text.ToString().Trim()))
    {
    Lblerr.Text = "selected date and excel sheet should be same";
    return;
    }
    }

    When i debug and check
    In selectedfrom date value is 9/2/2014 12:00:00 AM
    rows.cells[0].text.tostring(),trim() value is 1-Sep-14

    i want to change the format of selectedfrom date is 1-sep-14 format.

    like same as gridview date format 1-Sep-14.

    for that how can i do in asp.net using c#.
  • #750831
    Hello Rao,

    You just need to make a small change for this,

    In your code, change following


    DateTime selectedfromdate = Convert.ToDateTime(Txtfromdate.SelectedDateValue);


    to


    DateTime selectedfromdate = Convert.ToDateTime(Txtfromdate.SelectedDateValue.ToString("d-MMM-yy"));


    in ToString() Func you can specify any format for ex:-


    ddMMMyyyy, ddMMyyyy, ddMMyyyy HH:mm:ss, and so on..

    Regards,
    Shakil Sama

  • #750839
    Hi

    By default the excel cells are in General Format. You can change the cells in "text" format or Custom Date format (dd-mmm-yy). They you can try this...

    If you want to do this thru C# coding please visit the below link.

    http://www.c-sharpcorner.com/Forums/Thread/109064/change-excel-date-format-using-C-Sharp.aspx

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

  • #750856
    Please change this code as below



    DateTime selectedfromdate = Convert.ToDateTime(Txtfromdate.SelectedDateValue);
    for (int i = 0; i < GvSch.Rows.Count; i++)
    {
    if (selectedfromdate == Convert.ToDateTime(rows.Cells[0].Text.ToString("ddmmyyyy").Trim()))
    {
    Lblerr.Text = "selected date and excel sheet should be same";
    return;
    }
    }


    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #750878
    Hi,

    Use .ToString() method to convert your date field as you want.

    Ex:

    .ToString("dd-MMM-yyyy");


    Hope this will help you..

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

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #750911
    Hi Rao, use ToString() method to convert selected date value to dd-MMM-yy

    ex selectedfromdate.ToString("dd-MMM-yy")


    then selectedfromdate format is 1-Sep-14


  • Sign In to post your comments