|
-
September 25th, 2008, 10:49 AM
#1
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
-
September 25th, 2008, 11:29 AM
#2
Re: Format date WWYY
What you W be? mm/dd/yy is normal. mmdd or mmyy would be your choices
-
September 25th, 2008, 01:27 PM
#3
Re: Format date WWYY
I need the week and year... this week is 3908.
-
September 25th, 2008, 06:20 PM
#4
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.
-
September 25th, 2008, 07:31 PM
#5
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,
-
September 25th, 2008, 07:40 PM
#6
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
-
September 25th, 2008, 10:57 PM
#7
Re: Format date WWYY
in other words, impossible.
-
September 26th, 2008, 08:39 AM
#8
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( <ime );
today = localtime( <ime );
strftime(date_code,5,"%W%y",today);
There's no equivalent to this in VB?
-
September 27th, 2008, 08:52 PM
#9
Re: Format date WWYY
Msgbox format(now,"wwyy")
-
September 28th, 2008, 11:11 PM
#10
Re: Format date WWYY
oops. forgot to post back, after I saw 'ww'. then i remembered it
VB does the impossible, I guess.
-
September 29th, 2008, 12:41 PM
#11
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.
-
September 29th, 2008, 02:49 PM
#12
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.
-
September 29th, 2008, 04:59 PM
#13
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.
-
September 29th, 2008, 05:54 PM
#14
Re: Format date WWYY
works here. xp sp3 vb6-sp6
-
September 30th, 2008, 06:35 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|