在VBScript中计算给定时期的年周数

时间:2015-02-03 14:10:46

标签: vbscript asp-classic week-number datepart

情景:尝试使用给定的时间段找出结束周数(当前年份的周数)和开始周数。假设 PeriodWeeks = 10.这意味着从今天的周数到过去10周,这肯定会在去年的情况下进行。

代码我有:

perdiodWeeks = 10             ' this is a constant in the code
periodMonths= periodQtrs * 3  ' this calculates month from given number of quarters

endDate = DateAdd("m",-1,Date)
endYear = DatePart("yyyy", endDate)
endMonth = DatePart("m", endDate)

startDate = DateAdd("m", -(periodMonths-1), endDate)
startYear = DatePart("yyyy", startDate)
startMonth = DatePart("m", startDate)

如何计算以下内容?

startYW             ' start year week number
endYW               ' end year week number

其中,值为 endYW = 201506, startYW =来自endYW的perdiodWeeks。 06是2015年的当前周数

1 个答案:

答案 0 :(得分:1)

计算初始日期(减去#周* 7)并使用可用函数检索所需信息

Option Explicit

Dim periodWeeks
    periodWeeks = 10

Dim dateToday, dateStart
    dateToday = Now()
    dateStart = DateAdd("d",periodWeeks*-7, dateToday)

Dim weekToday, weekStart
    weekToday = DatePart("ww",dateToday)
    weekStart = DatePart("ww",dateStart)