-
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.
-
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" :(
-
1 Attachment(s)
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. :wave:
-
1 Attachment(s)
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
-
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?
-
Re: Date Comparison
Sorry. That was me looking for date comparisons for sorting.