Results 1 to 7 of 7

Thread: [RESOLVED] Date questions for the gurus

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved [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:
    1. 'Dim sEnd As String
    2. Dim sEnd As Date
    3.  
    4. Private Sub Timer1_Timer()
    5. 'Dim sStart As String
    6. Dim sStart As Date
    7. Dim sLabel As String
    8. Dim lDays&, lHours&, lMinute&, lSeconds&
    9.  
    10.     'sStart = Now
    11.     sStart = CDate(Now)
    12.     lSeconds = DateDiff("s", sStart, sEnd)
    13.    
    14.     lDays = lSeconds \ 3600 \ 24
    15.     lHours = lSeconds \ 3600 Mod 24
    16.     lMinute = ((lSeconds Mod 3600) \ 60)
    17.     lSeconds = ((lSeconds Mod 3600) Mod 60)
    18.    
    19.     sLabel = "Time left: " & _
    20.                 lDays & " days " & _
    21.                 lHours & " hours " & _
    22.                 lMinute & " minutes " & _
    23.                 lSeconds & " seconds"
    24.                
    25.     Label1.Caption = sLabel
    26. End Sub
    27.  
    28. Private Sub Form_Load()
    29.     'sEnd = Format(Date, "Short Date")
    30.     sEnd = CDate(Fix(Now))
    31.     sEnd = DateAdd("d", 2, sEnd)
    32.     Timer1.Enabled = True
    33. End Sub

    Thanks in advance,
    Ron

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. sEnd = DateAdd("d", 2, Date)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    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?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved 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!

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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!

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