Results 1 to 10 of 10

Thread: System Dates - Formating it

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    System Dates - Formating it

    since vb dates depend on how the system date is formated
    this is no good for me

    how can i make sure that the date will ALWAYS be same format

    "dd/mm/yyyy"
    with leading 0's

    such that its 07/25/2001

    this has to work for no matter WHAT the system date format is like
    some pick short, some pick long
    some reverse the month and day, ect
    i need

    DAY FIRST, MONTH, YEAR (with leading zeros)

    hope you guyz undrestand

    format(now, "dd/mm/yyyy") doesnt work for all cases of formating the system date manually do a different one

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Can you explain in what cases the Format function doesn'work? Because I have always been able to format it that way.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    ok
    i could SWEAR it wasnt working
    now it does
    hmm
    i will figure out whats going on
    the come back
    he he

    do you like writing code for people serge?
    if you do, i need some help he he

    given

    const STARTDATE = "01/01/1995"
    EndDate = format(now, "mm/dd/yyyy")

    so basically given a constatn start date
    and todays date
    i would like to populate a Treeview object as follow
    since 1995 is the start year
    i want it to start off with
    Code:
          1995
                Jan
                    1
                     2
                     3
                     ...(last day of that month for that year)
                 Feb
                     (same as above)
                 March (same as above
             
            1996 (same as above)
    
             ......
       
             2001 (same as above)
    however, since 2001 is not complete, i dont want all the 12 months to be listed, and since today is let say 25th, i dont want no dates to be listed after july 25th

    so the last date should be what EndDate is (logical)

    i know how to do this
    but i believe how i am doing this is not effecient so i am looking for a better solution

    here is how i am doing it

    sending in 2 dates into a class object
    then the class object figures out how many years there is in between (inclusive) the 2 dates
    and lists them (i will need this for the treeviews top nodes)
    but it gets tricky when comes to the months
    and how many days each of those months contain for that perticular year, i was gonna use a 2D array for month and what year they are, then i was gonna use a 3D array for day, month and year
    after thinking about this, the code would get ugly
    so i was hoping someone has a better solution
    or have time that they would like to help me out to do this (ya am asking you to write the code for me IF you like)

    attached is what i have done so far

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    It's fairly simply to do what you're asking, I also added the weekday name next to the day:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim intYear As Integer
    3.     Dim intMonth As Integer
    4.     Dim intDay As Integer
    5.     Dim nodYear As Node
    6.     Dim nodMonth As Node
    7.     Dim nodDay As Node
    8.     Dim intLastMonth As Integer
    9.     Dim intLastDay As Integer
    10.     Dim strTemp As String
    11.     Const STARTDATE = "01/01/1995"
    12.  
    13.    
    14.     With TreeView1
    15.         For intYear = Year(STARTDATE) To Year(Date)
    16.             Set nodYear = .Nodes.Add(, , "Year " & intYear, intYear)
    17.            
    18.             If intYear = Year(Date) Then
    19.                 intLastMonth = Month(Date)
    20.             Else
    21.                 intLastMonth = 12
    22.             End If
    23.            
    24.             For intMonth = 1 To intLastMonth
    25.                 Set nodMonth = .Nodes.Add(nodYear, tvwChild, , MonthName(intMonth))
    26.                
    27.                 If intMonth = Month(Date) And intYear = Year(Date) Then
    28.                     intLastDay = Day(Date)
    29.                 Else
    30.                     strTemp = Format("1/" & intMonth & "/" & intYear, "dd/mm/yyyy")
    31.                     intLastDay = GetlastDayofMonth(CDate(strTemp))
    32.                 End If
    33.                
    34.                 For intDay = 1 To intLastDay
    35.                     strTemp = intDay & "/" & intMonth & "/" & intYear
    36.                     Set nodDay = .Nodes.Add(nodMonth, tvwChild, , intDay & " (" & WeekdayName(Weekday(strTemp)) & ")")
    37.                 Next
    38.             Next
    39.         Next
    40.     End With
    41. End Sub

  5. #5

  6. #6
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Sorry, forgot about that function:
    VB Code:
    1. Public Function GetlastDayofMonth(p_Date As Date)
    2.     GetlastDayofMonth = Day(DateSerial(Year(p_Date), Month(p_Date) + 1, 0))
    3. End Function

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    thank you so much
    you saved me a lot of work
    i was gonna write a whole lota code for that

    tested it
    works like a charm (gonna make few modifications to how the days are dispalyed.. but thats about it )

    thanks again

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    just noticed a little bug

    if i change the start date in STARTDATE
    it doesnt work

    changing the year in it works fine
    so if i pick 2001 it does it properly

    but when i change the month and date
    it always goes Jan-EndDate (with all those values)
    even though if i had 07/20/2001, still puts jan-end date

    thanks

  9. #9
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    kovan, you can use the GetLocalelInfo and SetLocaleInfo to change the system date to any window recognize format you wish. Juz call this function when your app is loaded

    VB Code:
    1. Option Explicit
    2. '// WIN32 API Function
    3. Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
    4. Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long
    5.  
    6. '// WIN32 API Constant
    7. Private Const LOCALE_SYSTEM_DEFAULT = &H800
    8. Private Const LOCALE_USER_DEFAULT = &H400
    9. Private Const LOCALE_SSHORTDATE = &H1F        '  short date format string
    10. Private Const LOCALE_SLONGDATE = &H20         '  long date format stringPrivate Const LOCALE_USER_DEFAULT = &H400
    11. Private Const LOCALE_SDATE = &H1D             '  date separator
    12.  
    13. Private Sub Form_Load()
    14.     Dim sLocale As String
    15.     Dim retVal As Long
    16.     sLocale = Space(255)
    17.     retVal = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sLocale, Len(sLocale))
    18.     sLocale = Left(sLocale, retVal)
    19.     MsgBox "Old Short Date Format = : " & sLocale
    20.    
    21.     sLocale = "dd/MM/yyyy"
    22.     retVal = SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sLocale)
    23.     MsgBox "New Date Format = : " & sLocale
    24. End Sub
    regards,

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    thanks everyone
    got everything working just the way i want it

    oh ya that date going out of wack
    well it was my fault
    he he

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