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
Printable View
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
What you W be? mm/dd/yy is normal. mmdd or mmyy would be your choices
I need the week and year... this week is 3908.
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.
I think something like this may do the trick.
The offset value seems a bit odd but it seemed to work in the test I ran,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
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
in other words, impossible. :)
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:
There's no equivalent to this in VB?Code:char date_code[]={0,0,0,0,0};
time_t ltime;
struct tm *today;
time( <ime );
today = localtime( <ime );
strftime(date_code,5,"%W%y",today);
Msgbox format(now,"wwyy")
oops. forgot to post back, after I saw 'ww'. then i remembered it
VB does the impossible, I guess. :p
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. :blush:
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.
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.
works here. xp sp3 vb6-sp6
Odd indeed... it works OK on my VB6 but not on Visual Studio 2005 (which is what I'm using).
Thanks guys.