none
getting the only date from the datetime using c#

    Question

  • Hi,

     

    using the calender control ..i am getting the date in a textbox...and i have to insert this into database ..using asp.net with c#

    in the table also field is date..

    how can i do this..i did like this .but it is not inserting

     

    "+ DateTime.Parse(txtFinalDelivery.Text).ToString("yyyy-MM-dd")

     

    Ramesh

     

    Friday, June 29, 2007 5:46 AM

Answers


  • Use the following code:

    System.DateTime.Today.ToShortDateString()
    Friday, June 29, 2007 6:07 AM
  • Use parameter binding...

    eg:

    Code Snippet

    SqlCommand cmd = new SqlCommand("INSERT INTO foo ( birthday ) VALUES ( @birthday )");
    cmd.Parameters.Add("@birthday", SqlDbType.DateTime).Value = DateTime.Now;




    Friday, June 29, 2007 8:27 AM
  • DateTime.Parse(txtFinalDelivery.Text).ToString("yyyy-MM-dd") this will defenitly result only in date format, and I think it again get converted data time format when you try to insert into the database.

     

    Regards,

    Vinod S Nair

    Tuesday, July 03, 2007 6:06 AM

All replies


  • Use the following code:

    System.DateTime.Today.ToShortDateString()
    Friday, June 29, 2007 6:07 AM
  • Use parameter binding...

    eg:

    Code Snippet

    SqlCommand cmd = new SqlCommand("INSERT INTO foo ( birthday ) VALUES ( @birthday )");
    cmd.Parameters.Add("@birthday", SqlDbType.DateTime).Value = DateTime.Now;




    Friday, June 29, 2007 8:27 AM
  • I am facing the same dilemma... Save feature in my application in C# is build by the visual studio tool. So I am not writing any code for saving as its done automatically by the generated code. Now I have a datetime picker on my form which is binded to a date field in MS Access at the backend. Now I just want to save the date and not the time. This is causing me trouble as its saving the time also. How should I save just the date and not time.

     

    Any advise, suggestion will be highly appreciated.

     

    Monday, July 02, 2007 9:15 PM
  • Another thing you can do is simply instantiate a new DateTime, and send the constructor only Day/Month/Year from the original DateTime object.

    e.g.

    DateTime d = new DateTime(oldDT.Year,oldDT.Month,oldDT.Day)
    Monday, July 02, 2007 9:33 PM
  • DateTime.Parse(txtFinalDelivery.Text).ToString("yyyy-MM-dd") this will defenitly result only in date format, and I think it again get converted data time format when you try to insert into the database.

     

    Regards,

    Vinod S Nair

    Tuesday, July 03, 2007 6:06 AM
  • Well I was able to solve this issue in another way... I had to use the date from the Database to compare it to some present date. Well I modified the query to just get the date from the database rather than the whole date & time. So this solved my problem but its still saving the time in the Database.
    Tuesday, July 03, 2007 11:27 PM
    • Proposed as answer by Waqas Silat Thursday, August 16, 2012 6:47 PM
    Thursday, August 16, 2012 6:46 PM
  • Hello Amit,

    The above solution will definitely give only the date part but that will be in string format. I want to compare this date with an Excel date and comparing a string with an Excel date doesnot work. Can you please suggest me some other workaround to this problem.

    Rashmi.

    Thursday, October 18, 2012 3:46 PM
  • All of these methods seem to return string. What is the correct way to return Date portion only (and set time to 0) of the DateTime value?

    Although I think I will pass the date as is to T-SQL procedure and remove time portion there.

    Thanks in advance.

    BTW, can someone please move this thread to Visual C# forum as it's more like a language question?


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    • Edited by Naomi N Wednesday, February 13, 2013 4:07 PM
    Wednesday, February 13, 2013 4:06 PM
  • this will return string , but i need date type.
    Thursday, August 01, 2013 9:46 AM