CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Format date WWYY

    What's the simplest way to create a string with a 4 character WWYY representation of today's date? VB has so many date and format functions I can't figure out which to use.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Format date WWYY

    What you W be? mm/dd/yy is normal. mmdd or mmyy would be your choices
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: Format date WWYY

    I need the week and year... this week is 3908.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Format date WWYY

    I'd create a look up table every year. Otherwise, you have to figure out what day the first day of the year is, and then add 7 until your match is greater than day 0 and less than day 6.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Format date WWYY

    I think something like this may do the trick.


    Code:
    Private Sub Command1_Click()
    Dim Days As Integer
    Dim FirstDay As Integer
    Dim Offset As Integer
    Dim ThisWeek As Integer
    Dim ThisYear As String
     
    Days = DateDiff("d", "1/1/" & Year(Now), Now())
    FirstDay = Weekday("1/1/" & Year(Now))
    Offset = 6 - FirstDay
    ThisWeek = (Days + Offset) \ 7
     
    If Days + Offset Mod 7 > 0 Then
    	ThisWeek = ThisWeek + 1
    End If
     
    ThisYear = Right(Year(Now), 2)
     
    MsgBox Format(ThisWeek & ThisYear, "0#0#")
     
    End Sub
    The offset value seems a bit odd but it seemed to work in the test I ran,

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Format date WWYY

    You could also use the "ww" parameter in the datediff function to determine the number of weeks between the two dates then the weekday function to determine what the first day of the year was and the current day, then decide if 1 needs to be added to the value returned by datediff

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Format date WWYY

    in other words, impossible.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: Format date WWYY

    You gotta be kidding me. With all the date-time functions in VB there's no way to format a string with the current week number?

    In C++ I use this:
    Code:
    char date_code[]={0,0,0,0,0};
    time_t ltime;
    struct tm *today;
    time( &ltime );
    today = localtime( &ltime );
    strftime(date_code,5,"%W%y",today);
    There's no equivalent to this in VB?

  9. #9
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Format date WWYY

    Msgbox format(now,"wwyy")

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Format date WWYY

    oops. forgot to post back, after I saw 'ww'. then i remembered it

    VB does the impossible, I guess.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Format date WWYY

    Duh... I just assumed that since the first post actually had the wwyy as the format he was looking for that he had already tried that and it didn't work so I went looking for a workaround. Never even thought to try it for myself first.

  12. #12
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: Format date WWYY

    I did indeed try that, and it doesn't work. Format(now,"wwyy") returns "ww08". It seems that "ww" is not a recognized format qualifier.

    What I ended up doing is getting the week of year from DatePart:

    iNumberOfTheWeek = DatePart("ww", Now())

    and the 2 digit year from Format(Now, "yy") and concatonating them.

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Format date WWYY

    Odd.. it worked for me.. I just tryed it again and Format(Now,"wwyy") returns 4008

    I am using service pack 5 under XP pro

    Edit.. Make that under Win 2000. Forgot which PC I was using. Works on the XP box as well though.
    Last edited by DataMiser; September 29th, 2008 at 05:10 PM.

  14. #14
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Format date WWYY

    works here. xp sp3 vb6-sp6
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  15. #15
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: Format date WWYY

    Odd indeed... it works OK on my VB6 but not on Visual Studio 2005 (which is what I'm using).

    Thanks guys.

Page 1 of 2 12 LastLast

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

Featured