Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

VB6 = Week of the year in TextBox

Hello all

I'm trying to find a way to put in Text1 the week number if the year.

Ex:
Between Dec 30 2012 and January 5ft 2013, this =  Week 1
Between Jan 6 2012 and January 12 2013, this =  Week 2....


So when i load the form with my Text1, today, i would see 1 (Being week 1)

How can i do that.

Thanks again for your help
SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wilder1626

ASKER

I think i fount it.but i just need to tweak it cause i have week 53 on today's date but when i look at my company calendar, it count today as week 1 for 2013


 Dim iNumberOfTheWeek As Integer
    iNumberOfTheWeek = DatePart("ww", Now())
    MsgBox iNumberOfTheWeek

Open in new window

But if i do this, now i have week 1:

        Dim iNumberOfTheWeek As Integer
    iNumberOfTheWeek = DatePart("ww", Now() + 2)
    MsgBox iNumberOfTheWeek

Open in new window


weird.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't want to add 2 to Now() because you are looking up the week number for 2 days later which is Jan 1st.
Hi again.

Thank you so much both of you, again
all work great now.
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
Here is another, possibly the right way to obtain Week Of the Year

  'Syntax for DatePart:
  'DatePart (Interval, Date,  [FirstDayOfWeek, [FirstWeekOfYear]] )
  'The FirstWeekOfYear argument can have one of the following settings.
  '2 - Week that has at least four days in the new year (complies with ISO standard 8601, section 3.17)
  '1 - Week in which January 1 occurs (default)
  '0 - First week of year specified in system settings

  'iNumberOfTheWeek = DatePart("ww", Now(), vbMonday, 2)
  'MsgBox iNumberOfTheWeek

  'You can use GetLocaleInfo API to obtain FirstWeekOfYear Specifier
  iNumberOfTheWeek = DatePart("ww", Now(), vbMonday, GetMyLocaleInfo(LOCALE_IFIRSTWEEKOFYEAR))
  MsgBox iNumberOfTheWeek


Option Explicit
   
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

Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Private Const LOCALE_IFIRSTWEEKOFYEAR As Long = &H100D  'first week of year specifier

Private Function GetMyLocaleInfo( _
                    ByVal LCType As Long _
                    ) As String
                    
  Dim sReturn As String
  Dim iRet1 As Long
  Dim iRet2 As Long
  Dim lpLCDataVar As String
  Dim Pos As Integer
  Dim Locale As Long
   
  Locale = GetUserDefaultLCID()
  iRet1 = GetLocaleInfo(Locale, LCType, lpLCDataVar, 0)
   
  sReturn = String$(iRet1, 0)
  iRet2 = GetLocaleInfo(Locale, LCType, sReturn, iRet1)
  Pos = InStr(sReturn, Chr$(0))
  If Pos > 0 Then
     sReturn = Left$(sReturn, Pos - 1)
  End If
  
  GetMyLocaleInfo = sReturn
  
  Exit Function

End Function

Open in new window

Thanks eemit

This is also very good to know