CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: Date Comparison

  1. #16
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Date Comparison

    No idea why anyone would want to make it that complicated, as pointed out before by others a simple Date1< Date2 will do or if you want to know ho wmuch difference then a simple call to datediff() will do.

    There is no need for all the conversions, all it does is complicates the code and slows it down while also providing the possibilty of false results.
    Always use [code][/code] tags when posting code.

  2. #17
    Join Date
    Jan 2009
    Posts
    596

    Re: Date Comparison

    @DataMiser: I know, it really infuriates me when people spread complicated, slow, buggy ways of doing something which can be done easily, quickly and correctly. The reason I went into so much detail was because I was trying to convince Kenaso where he/she was going wrong - and, more importantly, to make sure that any inexperienced programmers reading this thread would not go away with the wrong idea.

    But Kenaso hasn't been online since his last reply, so won't even have read my reply to his last post. No doubt he has gone away still believing his way is better as it came from someone who "has been coding for quite some time and is very respected in the Visual Basic world"

  3. #18
    Join Date
    May 2012
    Posts
    7

    Thumbs up Re: Date Comparison

    Hi,
    This may be helpful for you.

    The following code's form is also attached with the reply.

    To get date difference between two dates in months, days and years and etc you can use the following code.
    Add two DTPickers and set values as per your requirement, you can use two date variables too to instead of these controls.
    Then compare these dates with the following lines of code


    Private Sub cmdCalculate_Click()
    lblYears.Caption = ""
    lblMonths.Caption = ""
    lblDays.Caption = ""
    lblQuarter.Caption = ""
    lblCalenderWeeks.Caption = ""
    lblWeeks.Caption = ""
    lblHours.Caption = ""
    lblMinutes.Caption = ""
    lblSeconds.Caption = ""

    lblYears.Caption = DateDiff("YYYY", dtpStartDate.Value, dtpEndDate.Value)
    lblMonths.Caption = DateDiff("M", dtpStartDate.Value, dtpEndDate.Value)
    lblDays.Caption = DateDiff("D", dtpStartDate.Value, dtpEndDate.Value)
    lblQuarter.Caption = DateDiff("Q", dtpStartDate.Value, dtpEndDate.Value)
    lblCalenderWeeks.Caption = DateDiff("WW", dtpStartDate.Value, dtpEndDate.Value)
    lblWeeks.Caption = DateDiff("W", dtpStartDate.Value, dtpEndDate.Value)
    lblHours.Caption = DateDiff("H", dtpStartDate.Value, dtpEndDate.Value)
    lblMinutes.Caption = DateDiff("N", dtpStartDate.Value, dtpEndDate.Value)
    lblSeconds.Caption = DateDiff("S", dtpStartDate.Value, dtpEndDate.Value)
    End Sub

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 Then
    End
    End If
    End Sub

    Private Sub Form_Load()
    dtpStartDate.Value = Date
    dtpEndDate.Value = Date
    End Sub

    In case of any issue please do revert back.
    Attached Files Attached Files

  4. #19
    Join Date
    May 2012
    Posts
    7

    Thumbs up Re: Date Comparison

    Private Sub cmdCalculate_Click()
    lblYears.Caption = ""
    lblMonths.Caption = ""
    lblDays.Caption = ""
    lblQuarter.Caption = ""
    lblCalenderWeeks.Caption = ""
    lblWeeks.Caption = ""
    lblHours.Caption = ""
    lblMinutes.Caption = ""
    lblSeconds.Caption = ""

    lblYears.Caption = DateDiff("YYYY", dtpStartDate.Value, dtpEndDate.Value)
    lblMonths.Caption = DateDiff("M", dtpStartDate.Value, dtpEndDate.Value)
    lblDays.Caption = DateDiff("D", dtpStartDate.Value, dtpEndDate.Value)
    lblQuarter.Caption = DateDiff("Q", dtpStartDate.Value, dtpEndDate.Value)
    lblCalenderWeeks.Caption = DateDiff("WW", dtpStartDate.Value, dtpEndDate.Value)
    lblWeeks.Caption = DateDiff("W", dtpStartDate.Value, dtpEndDate.Value)
    lblHours.Caption = DateDiff("H", dtpStartDate.Value, dtpEndDate.Value)
    lblMinutes.Caption = DateDiff("N", dtpStartDate.Value, dtpEndDate.Value)
    lblSeconds.Caption = DateDiff("S", dtpStartDate.Value, dtpEndDate.Value)
    End Sub

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 Then
    End
    End If
    End Sub

    Private Sub Form_Load()
    dtpStartDate.Value = Date
    dtpEndDate.Value = Date
    End Sub
    Attached Files Attached Files

  5. #20
    Join Date
    Mar 2006
    Posts
    16

    Re: Date Comparison

    Hello. I have been working the late shift and spending my off hours searching for pin holes in my eye lids. I am trying to get caught up on the discussion. How goes the sorting?

  6. #21
    Join Date
    Mar 2006
    Posts
    16

    Re: Date Comparison

    Sorry. That was me looking for date comparisons for sorting.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured