L?I: ?nh d?ng ho?c ch?c nng DatePart th? tr? l?i sai tu?n s? cho cu?i th? hai trong nm

D?ch tiu ? D?ch tiu ?
ID c?a bi: 200299 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TRI?U CH?NG

Khi b?n s? d?ng ho?c cc inh dang ho?c DatePart ch?c nng ? xc ?nh s? tu?n cho ngy thng b?ng cch s? d?ng c php sau:
Format(AnyDate, "ww", vbMonday, vbFirstFourDays)

DatePart("ww", AnyDate, vbMonday, vbFirstFourDays)
				
cu?i th? hai trong m?t s? nm l?ch tr? l?i nh tu?n 53 khi n nn l trong tu?n 1.

NGUYN NHN

Khi xc ?nh s? tu?n m?t ngy theo tiu chu?n ISO 8601, cc cu?c g?i ch?c nng ti?m ?n cho t?p tin Oleaut32.dll nh?m l?n tr? v? tu?n 53 thay v? c?a tu?n 1 cho cu?i th? hai trong m?t s? nm.

GI?I PHP

S? d?ng m?t ch?c nng ng?i dng ?nh ngh?a ? tr? v? s? tu?n d?a trn cc quy t?c cho cc tiu chu?n ISO 8601. M?t v d? ?c bao g?m trong bi vi?t ny.

T?NH TR?NG

Microsoft ? xc nh?n r?ng y l m?t v?n ? trong t?p tin Oleaut32.dll.

THNG TIN THM

Cc tiu chu?n ISO 8601 ?c s? d?ng r?ng r?i ? chu u v bao g?m:
  ISO 8601 "Data elements and interchange formats - Information interchange   - Representation of dates and times"
  ISO 8601 : 1988 (E) paragraph 3.17:
  "week, calendar: A seven day period within a calendar year, starting
  on a Monday and identified by its ordinal number within the year;
  the first calendar week of the year is the one that includes the
  first Thursday of that year. In the Gregorian calendar, this is
  equivalent to the week which includes 4 January."
				
i?u ny c th? ?c th?c hi?n b?ng cch p d?ng nh?ng quy t?c ny cho l?ch tu?n:
  • M?t nm ?c chia thnh 52 ho?c 53 tu?n l?ch.
  • M?t tu?n l?ch c 7 ngy. Th? hai ngy 1, ch? nh?t l 7 ngy.
  • Tu?n l?ch ?u tin c?a m?t nm l m?t trong c ch?a t nh?t 4 ngy.
  • N?u m?t nm khng ph?i k?t thc vo ngy Ch? Nh?t, ngy c?a n 1-3 cu?i cng thu?c v? nm t?i ?u tin l?ch tu?n ho?c 1-3 ngy ?u tin c?a nm t?i thu?c v? c?a nm nay l?ch tu?n tr?c.
  • Ch? l m?t nm b?t ?u ho?c k?t lu?n ngy th? nm c 53 l?ch tu?n.
Trong Visual Basic v Visual Basic cho cc ?ng d?ng, t?t c? cc ngy ch?c nng, ngo?i tr? cc DateSerial ch?c nng, ?n t? cc cu?c g?i ?n t?p tin Oleaut32.dll. B?i v? c? hai cc Format() v DatePart() ch?c nng c th? tr? l?i l?ch tu?n s? cho m?t ngy nh?t ?nh, c? hai ang b? ?nh h?ng b?i l?i ny. ? trnh v?n ? ny, b?n ph?i s? d?ng m? thay th? bi vi?t ny cung c?p.

Cc b?c ? t?o l?i hnh vi

  1. B?t ?u m?t d? n tiu chu?n EXE trong Visual Basic. Form1 ?c t?o ra theo m?c ?nh.
  2. Thm hai CommandButtons ? Form1.
  3. Dn o?n m? sau vo c?a s? m? c?a Form1:
    Option Explicit
    
    Private Sub Command1_Click()
    ' This code tests a "problem" date and the days around it
    Dim DateValue As Date
    Dim i As Integer
    
    Debug.Print "   Format function:"
    DateValue = #12/27/2003#
    For i = 1 To 4   ' examine the last 4 days of the year
        DateValue = DateAdd("d", 1, DateValue)
        Debug.Print "Date: " & DateValue & "   Day: " & _
          Format(DateValue, "ddd") & "   Week: " & _
          Format(DateValue, "ww", vbMonday, vbFirstFourDays)
    Next i
    End Sub
    
    Private Sub Command2_Click()
    ' This code lists all "Problem" dates within a specified range
      Dim MyDate As Date
      Dim Years As Long
      Dim days As Long
      Dim woy1 As Long
      Dim woy2 As Long
      Dim ToPrint As String
    
      For Years = 1850 To 2050
        For days = 0 To 3
          MyDate = DateSerial(Years, 12, 28 + days)
          woy1 = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
          woy2 = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
          If woy2 > 52 Then
            If Format(MyDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then _
              woy2 = 1
          End If
          If woy1 <> woy2 Then
            ToPrint = MyDate & String(13 - Len(CStr(MyDate)), " ")
            ToPrint = ToPrint & Format(MyDate, "dddd") & _
              String(10 - Len(Format(MyDate, "dddd")), " ")
            ToPrint = ToPrint & woy1 & String(5 - Len(CStr(woy1)), " ")
            ToPrint = ToPrint & woy2
            Debug.Print ToPrint
          End If
        Next days
      Next Years
    End Sub
    					
  4. Gi? phm CTRL v ?n phm g ? m? c?a s? ngay l?p t?c.
  5. Ch?y cc d? n, h?y nh?p vo Command1, v lu ? cc k?t qu? sau trong c?a s? ngay l?p t?c:
       Format function:
    Date: 12/28/03   Day: Sun   Week: 52
    Date: 12/29/03   Day: Mon   Week: 53
    Date: 12/30/03   Day: Tue   Week: 1
    Date: 12/31/03   Day: Wed   Week: 1
    						
    Lu ? r?ng v?i ?nh d?ng ny, t?t c? cc tu?n b?t ?u v?i th? hai, ? 12/29/2003 c?n ?c xem xt s? b?t ?u c?a tu?n 1 v khng ph?i l m?t ph?n c?a tu?n 53.
  6. Nh?p vo Command2 ? xem danh sch cc ngy trong d?y ni ? ch? ra r?ng kinh nghi?m v?n ? ny. Danh sch bao g?m ngy, tu?n ngy (lun lun th? hai), tu?n # tr? l?i b?ng ?nh d?ng (53), v m?t s? tu?n n nn tr? v? (1.) Vi du:
    12/29/1851   Monday    53   1
    12/31/1855   Monday    53   1
    12/30/1867   Monday    53   1
    12/29/1879   Monday    53   1
    12/31/1883   Monday    53   1
    12/30/1895   Monday    53   1
    ...
    					

Cch gi?i quy?t

N?u b?n s? d?ng cc inh dang ho?c DatePart ch?c nng, b?n c?n ki?m tra gi tr? tr? l?i v, khi n l 53, ch?y m?t ki?m tra v p bu?c m?t tr? l?i c?a 1, n?u c?n thi?t. M?u m? ny ch?ng t? m?t cch ? lm i?u ny:
Function WOY (MyDate As Date) As Integer   ' Week Of Year
  WOY = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
  If WOY > 52 Then
    If Format(MyDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then WOY = 1
  End If
End Function
				
B?n c th? trnh b?ng cch s? d?ng cc ch?c nng ny ? xc ?nh s? l?ng tu?n b?ng cch vi?t m? m th?c hi?n cc quy t?c ISO 8601 m t? ? trn. V d? sau y ch?ng t? m?t ch?c nng thay th? ? tr? v? s? tu?n.

V d? t?ng b?c

  1. B?t ?u m?t d? n tiu chu?n EXE trong Visual Basic. Form1 ?c t?o ra theo m?c ?nh.
  2. T? cc D an Menu, thm m?t m-un m?i v sau dn vo m? sau y:
    Option Explicit
    
    Function WeekNumber(InDate As Date) As Integer
      Dim DayNo As Integer
      Dim StartDays As Integer
      Dim StopDays As Integer
      Dim StartDay As Integer
      Dim StopDay As Integer
      Dim VNumber As Integer
      Dim ThurFlag As Boolean
    
      DayNo = Days(InDate)
      StartDay = Weekday(DateSerial(Year(InDate), 1, 1)) - 1
      StopDay = Weekday(DateSerial(Year(InDate), 12, 31)) - 1
      ' Number of days belonging to first calendar week
      StartDays = 7 - (StartDay - 1)
      ' Number of days belonging to last calendar week
      StopDays = 7 - (StopDay - 1)
      ' Test to see if the year will have 53 weeks or not
      If StartDay = 4 Or StopDay = 4 Then ThurFlag = True Else ThurFlag = False
      VNumber = (DayNo - StartDays - 4) / 7
      ' If first week has 4 or more days, it will be calendar week 1
      ' If first week has less than 4 days, it will belong to last year's
      ' last calendar week
      If StartDays >= 4 Then 
         WeekNumber = Fix(VNumber) + 2 
      Else 
         WeekNumber = Fix(VNumber) + 1
      End If
      ' Handle years whose last days will belong to coming year's first 
      ' calendar week
      If WeekNumber > 52 And ThurFlag = False Then WeekNumber = 1
      ' Handle years whose first days will belong to the last year's 
      ' last calendar week
      If WeekNumber = 0 Then
         WeekNumber = WeekNumber(DateSerial(Year(InDate) - 1, 12, 31))
      End If
    End Function
    
    Function Days(DayNo As Date) As Integer
      Days = DayNo - DateSerial(Year(DayNo), 1, 0)
    End Function
    					
  3. Thm m?t CommandButton ? Form1.
  4. Dn o?n m? sau vo c?a s? m? c?a Form1:
    Private Sub Command1_Click()
       Dim DateValue As Date, i As Integer
       
       Debug.Print "   WeekNumber function:"
       DateValue = #12/27/2003#
       For i = 1 To 4   ' examine the last 4 days of the year
           DateValue = DateAdd("d", 1, DateValue)
           Debug.Print "Date: " & DateValue & "   Day: " & _
              Format(DateValue, "ddd") & "   Week: " & WeekNumber(DateValue)
       Next i
    End Sub
    					
  5. Gi? phm CTRL v ?n phm g ? m? c?a s? ngay l?p t?c.
  6. Ch?y cc d? n v nh?p vo Command1 ? xem cc k?t qu? sau trong c?a s? ngay l?p t?c:
       WeekNumber function:
    Date: 12/28/03   Day: Sun   Week: 52
    Date: 12/29/03   Day: Mon   Week: 1
    Date: 12/30/03   Day: Tue   Week: 1
    Date: 12/31/03   Day: Wed   Week: 1
    					
    Lu ? r?ng th? hai ?c coi l tu?n 1 nh n nn.

Thu?c tnh

ID c?a bi: 200299 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic for Applications 6.0
T? kha:
kbbug kbdatetime kbpending kbmt KB200299 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:200299

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com