[RESOLVED] Date questions for the gurus
Hey All,
I'm working on a little "countdown to a certain date" code (found here).
After searching this forum for the last couple of hours, I'm confused about
the regional settings.
I've read stuff about Bulgarian settings, this Format, that Format, you should
Format, you shouldn't Format. I just don't want people in other countries
to have any problems.
My questions are...
1. Does the Now function always return the Short Date format? (it does for
me)
2. Is there anything wrong with the code below? (works for me)
VB Code:
'Dim sEnd As String
Dim sEnd As Date
Private Sub Timer1_Timer()
'Dim sStart As String
Dim sStart As Date
Dim sLabel As String
Dim lDays&, lHours&, lMinute&, lSeconds&
'sStart = Now
sStart = CDate(Now)
lSeconds = DateDiff("s", sStart, sEnd)
lDays = lSeconds \ 3600 \ 24
lHours = lSeconds \ 3600 Mod 24
lMinute = ((lSeconds Mod 3600) \ 60)
lSeconds = ((lSeconds Mod 3600) Mod 60)
sLabel = "Time left: " & _
lDays & " days " & _
lHours & " hours " & _
lMinute & " minutes " & _
lSeconds & " seconds"
Label1.Caption = sLabel
End Sub
Private Sub Form_Load()
'sEnd = Format(Date, "Short Date")
sEnd = CDate(Fix(Now))
sEnd = DateAdd("d", 2, sEnd)
Timer1.Enabled = True
End Sub
Thanks in advance,
Ron
Re: Date questions for the gurus
1. Believe it or not, it doesn't return any format!
It returns a Date value, which is actually stored as a number. It is only formatted when you convert it to a string (eg: by putting it into a textbox), and then it is formatted using Short Date as you have found.
2. The only thing which looks (very) dubious is Fix - but you dont need that line anyway, you can simply change the next line to this:
VB Code:
sEnd = DateAdd("d", 2, Date)
Re: Date questions for the gurus
Hey si_the_geek... thanks for responding.
So, if I change that line you gave me...I shouldn't have any regional
settings conflict, is that correct?
Re: Date questions for the gurus
It will all be fine - as you are only using the Date data type (as opposed to the Date function in my code!) to store date values.
You only need to worry if dates are stored as text at any point.
Re: Date questions for the gurus
In addition to what si posted
Problems arise when dates get converted to/from strings improperly. For example, this code will print different results for different regional settings. Is 01/02/2006 Jan 2 or Feb 1?
Debug.Print DateDiff("d", "01-Jan-2006", "01/02/2006")
But your code does not convert strings to dates and so should work no matter the regional setting.
Re: Date questions for the gurus
I didn't know that was your code I found...works very well. :)
I also didn't know the Now function returned a Date value.
Thanks alot guys, I appreciate your help...have a good one!
Re: Date questions for the gurus
I'm glad to help. :)
Quote:
Originally Posted by rdcody
I didn't know that was your code I found...
I don't think it was (I hate the & syntax for declarations!), I was actually refering (not too clearly) to my one-liner! :blush: