Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click Dim date1 As Date = txtDate.Text Dim date0 = Year(date1) "/01/01" Dim wk = DateDiff("ww", date0, date1, Microsoft.VisualBasic.FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFullWeek) If (wk = 0) Then //判斷周數(shù)是否為0,為0則為去年最后一周 Dim date2 = Year(date1) - 1 Dim date3 = date2 "/12/31" Dim date4 = date2 "/01/01" wk = DateDiff("ww", date4, date3, Microsoft.VisualBasic.FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFullWeek) End If lblShow.Text = txtDate.Text "是" Year(date1) "第" wk "周!"End Sub
目前創(chuàng)新互聯(lián)建站已為上千余家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、網(wǎng)站托管、服務器托管、企業(yè)網(wǎng)站設計、仁化網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Dim ThisDay As String = Format(Now, "yyyy-MM-dd") '獲得當前日期字符串
Dim ThisDateTime As DateTime = Convert.ToDateTime(ThisDay) '當前日期轉(zhuǎn)換成DateTime
Dim ThisWeekDay As Integer = ThisDateTime.DayOfWeek '獲得當前日期是星期幾
Dim differadd As Integer = 1 - ThisWeekDay '相差的天數(shù)(星期1與當前星期幾相差的天數(shù))
Dim MyAdd As New TimeSpan(differadd, 0, 0, 0)
Dim MyYear As Integer = Format(Now, "yyyy") '獲取當前日期的年份
Dim MyMonth As Integer = Format(Now, "MM") '獲取當前日期的月份
Dim MyDay As Integer = Format(Now, "dd") '獲取當前日期是幾號
Dim MyToday As DateTime = New DateTime(MyYear, MyMonth, MyDay)
Dim Yourday As DateTime = MyToday.Add(MyAdd)
MsgBox("本周星期一的日期是:" Yourday)
Public?Function?IsRunnian(ByVal?n?As?Integer)?As?Boolean
Dim?res?As?Boolean
res?=?False
If?n?Mod?400?=?0?Then
res?=?True
End?If
If?(n?Mod?100??0)?And?(n?Mod?4?=?0)?Then
res?=?True
End?If
IsRunnian?=?res
End?Function
Public?Function?Fc(ByVal?td?As?Date)?As?Byte
Dim?y?As?Integer
Dim?m?As?Integer
Dim?d?As?Integer
Dim?yc?As?Integer
Dim?mc?As?Integer
Dim?dc?As?Integer
y?=?Year(td)
m?=?Month(td)
d?=?Day(td)
dc?=?d
If?IsRunnian(y)?Then
mc?=?Val(Mid("512503514624",?m,?1))
Else
mc?=?Val(Mid("622503514624",?m,?1))
End?If
yc?=?((y?Mod?100)?\?4?+?(y?Mod?100))?Mod?7
Fc?=?(yc?+?mc?+?dc)?Mod?7
End?Function
VB內(nèi)置的Weekday函數(shù)就可以返回星期.
Weekday(date[, firstdayofweek])
-- date: 必要; Date類型; 要求的日期
-- firstdayofweek: 可選; vbDayOfWeek類型; 定義每周第一天(默認為星期天)
Weekday返回一個Integer, 代表每周第幾天.
============================================
WeekdayName(weekday[, abbreviate][, firstdayofweek])
-- weekday: 必要; Long類型; 每周第幾天
-- abbreviate: 可選; Boolean類型; 表示星期的名稱是否被縮寫(默認為False)
-- firstdayofweek: 可選; vbDayOfWeek類型; 定義每周第一天(默認為星期天)
WeekdayName返回一個String, 如"星期一", 但根據(jù)不同的系統(tǒng), 也會返回如"Monday".
============================================
你也可以不用WeekdayName自己得到星期名
Choose(Weekday(#8/8/2008#, vbMonday), "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日")
這里的“某一周”是有歧義的:
一種可能是某日期所在的周
第二種是某年的第幾周。這還有歧義。因為某年的第一周,可能開始于上一年,同時這一周也是上一年的最后一周,會重復計算。我們這里假設“不管是否重復計算”
第三種是每周從星期幾開始。我們這里假設“從星期一開始”。
下面分別是輸出“某日期所在的周始終日期”、“某年某周始終日期”、“某年各周始終日期”的代碼!
如果每周從星期日開始,將vbMonday替換為vbSunday即可。
'某日期所在的周始終日期
Private Sub Command3_Click()
Dim dt As Date
Dim wd As Integer
dt = InputBox("請輸入一個日期(Date格式):", "輸入日期", Date)
wd = Weekday(dt, vbMonday) '某日是星期幾,周一為第一天
firstDay = DateAdd("d", -wd + 1, dt) '本周的開始日期
endDay = DateAdd("d", 7 - wd, dt) '本周的開始日期
Text1.Text = "當前日期所在的周起止日期:"
Text1.Text = Text1.Text vbNewLine
Text1.Text = Text1.Text vbNewLine "每周從星期一開始"
Text1.Text = Text1.Text vbNewLine firstDay vbTab endDay
wd = Weekday(dt, vbSunday) '某日是星期幾,周日為第一天
firstDay = DateAdd("d", -wd + 1, dt) '本周的開始日期
endDay = DateAdd("d", 7 - wd, dt) '本周的開始日期
Text1.Text = Text1.Text vbNewLine
Text1.Text = Text1.Text vbNewLine "每周從星期日開始"
Text1.Text = Text1.Text vbNewLine firstDay vbTab endDay
End Sub
'某年某周始終日期
Private Sub Command4_Click()
Dim y As Integer
Dim w As Integer
Dim dt As Date
Dim wd As Integer
y = InputBox("請輸入年份:", "輸入年份", Year(Date))
dt = DateSerial(y, 1, 1)
w = InputBox("請輸入周數(shù):", "第幾周", "")
dt = DateAdd("ww", (w - 1), dt)
wd = Weekday(dt, vbMonday) '某日是星期幾,周一為第一天
firstDay = DateAdd("d", -wd + 1, dt) '本周的開始日期
endDay = DateAdd("d", 7 - wd, dt) '本周的開始日期
Text1.Text = "當前日期所在的周起止日期:"
Text1.Text = Text1.Text vbNewLine
Text1.Text = Text1.Text vbNewLine "每周從星期一開始"
Text1.Text = Text1.Text vbNewLine firstDay vbTab endDay
End Sub
'某年各周始終日期
Private Sub Command2_Click()
Dim y As Integer
Dim w As Integer
Dim dt As Date
Dim dte As Date
Dim wd As Integer
y = InputBox("請輸入年份:", "輸入年份", Year(Date))
dt = DateSerial(y, 1, 1)
dte = DateAdd("yyyy", 1, dt) - 1 '某年最后一天
wd = Weekday(dt, vbMonday) '某日是星期幾,周一為第一天
firstDay = DateAdd("d", -wd + 1, dt) '本周的開始日期
endDay = firstDay + 6 '本周的開始日期
Text1.Text = "當前日期所在的周起止日期:"
Text1.Text = Text1.Text vbNewLine "(每周從星期一開始)"
Text1.Text = Text1.Text vbNewLine
Text1.Text = Text1.Text vbNewLine firstDay vbTab endDay
Do While endDay dte
w = w + 1
firstDay = firstDay + 7
endDay = firstDay + 6
Text1.Text = Text1.Text vbNewLine firstDay vbTab endDay
Loop
End Sub