Re: Calculating a time and date (counting down)
- From: "Steve Gerrard" <mynamehere@xxxxxxxxxxx>
- Date: Sat, 19 Jan 2008 19:48:46 -0800
Steve Gerrard wrote:
Rick Rothstein (MVP - VB) wrote:
Another slight bug. Blame this on Bobzter100 :)
It reports that March 20, 2008 is 1 month, 4 weeks, 2 days away.
A new version, which I think avoids the bugs.
' the demo
Dim When As Date
Private Sub Form_Load()
When = DateAdd("d", 1, DateAdd("m", 2, Date)) _
+ TimeSerial(Hour(Now), Minute(Now) + 2, 0)
Me.Caption = When
End Sub
Private Sub Timer1_Timer()
Label1.Caption = YMWDHMS(When)
End Sub
' the functions
Function YMWDHMS(ByVal Target As Date) As String
Dim Ref As Date
Dim Secs As Long
Dim Mins As Long
Dim Hrs As Long
Dim Days As Long
Dim Weeks As Long
Dim Months As Long
Dim Years As Long
Ref = Now
If Ref > Target Then
Ref = Target
Target = Now
End If
Secs = DateDiff("s", Ref, Target)
Mins = Secs \ 60: Secs = Secs - Mins * 60
Hrs = Mins \ 60: Mins = Mins - Hrs * 60
Days = Hrs \ 24: Hrs = Hrs - Days * 24
Ref = Fix(Ref)
Target = DateAdd("d", Days, Ref)
Months = DateDiff("m", Ref, Target)
Years = Months \ 12: Months = Months - Years * 12
Ref = DateAdd("m", Months, DateAdd("yyyy", Years, Ref))
Days = DateDiff("d", Ref, Target)
Weeks = Days \ 7: Days = Days - Weeks * 7
YMWDHMS = FormatUnit(Years, "year") & ", " _
& FormatUnit(Months, "month") & ", " _
& FormatUnit(Weeks, "week") & ", " _
& FormatUnit(Days, "day") & ", " _
& FormatUnit(Hrs, "hour") & ", " _
& FormatUnit(Mins, "minute") & ", " _
& FormatUnit(Secs, "second")
End Function
Private Function FormatUnit(Num As Long, Unit As String) As String
FormatUnit = CStr(Num) & " " & Unit & IIf(Num = 1, "", "s")
End Function
.
- Follow-Ups:
- Re: Calculating a time and date (counting down)
- From: Twayne
- Re: Calculating a time and date (counting down)
- From: Bobzter100
- Re: Calculating a time and date (counting down)
- References:
- Re: Calculating a time and date (counting down)
- From: Rick Rothstein \(MVP - VB\)
- Re: Calculating a time and date (counting down)
- From: Steve Gerrard
- Re: Calculating a time and date (counting down)
- Prev by Date: Re: VB6 (formless) Timer class
- Next by Date: Re: How's dot.net doing nowadays?
- Previous by thread: Re: Calculating a time and date (counting down)
- Next by thread: Re: Calculating a time and date (counting down)
- Index(es):
Relevant Pages
|