Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Week Number two digits 2

Status
Not open for further replies.

ensorg

IS-IT--Management
Jan 9, 2002
229
0
0
GB
Hi All,

I have a bit of VBA code to format a date into a year and week number format...

intweekstartn = Format(datestart, "yyyyww", 2)

So today would be 200627

The problem is if the week number is a single digit then it drops the leading zero, so it is returned as 20061 and not 200601. This causes a problem later in my code.

I "fixed" this with a few lines of code where I checked the length of what had been returned and if it was only 5 digits I added in the zero. But, I was wondering if there was a way to get format to always return the week number as 2 digits?

Thanks
 
And what about this ?
intweekstartn = Format(datestart, "yyyy") * 100 + Format(datestart, "ww", 2)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This works for me

intweekstartn = Format(datestart, "yyyy0ww", 2)
 
Oh no it doesn't - puts in the zero even if week is > 9 - sorry 'bout that
 

"So today would be 200627" Where's the ZERO???

"...always return the week number as 2 digits?"

Where did YOU get this [red]ZERO[/red]???
[tt]
intweekstartn = Format(datestart, "yyyy[red]0[/red]ww", 2)[/tt]
Sorry?

Sorry for what?


Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Skip, take your medication and lie down for a bit.

The original poster claimed that week numbers of less than 10 were single digit week number, instead of double digit ( i.e. with a leadingzero ) ... so today would be 200627 ( correct ).

Then taupirho tried a number format with an embedded zero, which was incorrect ( which you spotted ), which is why taupirho said sorry ... for creating an incorrect bit code. That's what the sorry bit was about. Have you been under a bit of stress lately?????? ;-)

:-D


Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
intweekstartn = Year(adate) & Format(Format(adate, "ww"), "00")
 
Stongm, That works perfectly. Thank you.
 
ensorg, you didn't try my suggestion posted 4 Jul 06 5:58 ?
 
PHV, apologies, I looked at it quickly and thought it didn't work, but after taking my time and looking at it again it does work perfectly. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top