locked
difference between Two dates value RRS feed

  • Question

  • hello i need info ,to calculate value between to dates and compare it with a value = 15days

     Cmd.CommandText = "SELECT *FROM location WHERE  (lastdate -  firstdate >=10

    because i don't get the right conversion of date to be a value

    Wednesday, May 25, 2016 6:00 PM

Answers

  • In SQL Server you can use the DATEFIFF function:

    SELECT * FROM location WHERE DATEDIFF(day, lastdate, firstdate) >= 10

    But please ask any further SQL Server related questions you may have in the right forum: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:49 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Wednesday, May 25, 2016 7:08 PM
  • SOLVED i used:


                TimeSpan difference = dateTimePicker1.Value - dateTimePicker2.Value;
                if (Math.Round(difference.TotalDays) >= 10)

                    textBox8.Text = difference.TotalDays.ToString();

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:49 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Wednesday, May 25, 2016 8:25 PM
  • If you want to compare the differences in days between two C# DateTime objects, you could use the Subtract method:

    if((int)dateTimePicker1.Value.Subtract(dateTimePicker1.Value).TotalDays >= 10)
                {
    //...
                }

    But please be specific about your issue when starting a new thread.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:50 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Friday, May 27, 2016 8:50 PM

All replies

  • In SQL Server you can use the DATEFIFF function:

    SELECT * FROM location WHERE DATEDIFF(day, lastdate, firstdate) >= 10

    But please ask any further SQL Server related questions you may have in the right forum: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?category=sqlserver

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:49 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Wednesday, May 25, 2016 7:08 PM
  • SOLVED i used:


                TimeSpan difference = dateTimePicker1.Value - dateTimePicker2.Value;
                if (Math.Round(difference.TotalDays) >= 10)

                    textBox8.Text = difference.TotalDays.ToString();

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:49 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Wednesday, May 25, 2016 8:25 PM
  • If you want to compare the differences in days between two C# DateTime objects, you could use the Subtract method:

    if((int)dateTimePicker1.Value.Subtract(dateTimePicker1.Value).TotalDays >= 10)
                {
    //...
                }

    But please be specific about your issue when starting a new thread.

    Hope that helps.

    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Albert_Zhang Thursday, June 2, 2016 4:50 PM
    • Marked as answer by DotNet Wang Monday, June 6, 2016 3:18 AM
    Friday, May 27, 2016 8:50 PM