【问题标题】:To dispaly number of weeks from starting date to ending date显示从开始日期到结束日期的周数
【发布时间】:2020-03-18 09:05:33
【问题描述】:

我想给出开始日期和结束日期作为输入,我需要这些日期之间的周数列表。 例如: 如果我将开始日期设为 01/11/2019,结束日期设为 14/12/2019,我的输出将是

1
2
3
4
5
1
2
3

(因为前 5 个是 11 月的几周,接下来的 3 个是 12 月的几周)......让我们看另一个例子:如果我将开始日期指定为 14/11/2019 并将结束日期指定为2019 年 12 月 14 日,我的输出将是

3
4
5
1
2
3

(因为前 3 个是 11 月的几周,接下来的 3 个是 12 月的几周)....

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME
DECLARE @CurrentDate AS DATETIME
SET @StartDate = '2019-11-01'
SET @EndDate = '2019-12-14'
SET @CurrentDate = @StartDate
WHILE (@CurrentDate < @EndDate)
BEGIN
Print datepart(day, datediff(day, 0, @CurrentDate)/7 * 7)/7 + 1
SET @CurrentDate = DATEADD(DAY, 7, @CurrentDate); 
END

【问题讨论】:

  • MySQL != SQL Server。请仅标记您实际使用的RDBMS。
  • 你做了什么发布。
  • 请只添加相关标签,标签列表不是您的简历。
  • 如何定义一个月内的“周数”?即很明显,每月的第一天总是在第一周……但是第二周什么时候开始? - 即您对日期范围 10 nov 20193 Dec 201911 Nov 20188 Dec 2019 的期望是什么

标签: javascript c# sql asp.net


【解决方案1】:

你可以试试下面的脚本-

DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME
DECLARE @CurrentDate AS DATETIME
SET @StartDate = '2019-11-01'
SET @EndDate = '2019-12-14'
SET @CurrentDate = @StartDate


WHILE (@CurrentDate < @EndDate)
BEGIN

IF datepart(DD,@CurrentDate) <=7 
BEGIN 
    SET @CurrentDate = DATEADD(DD,-(DATEPART(DD,@CurrentDate)-1),@CurrentDate) 
END 


PRINT datepart(dd,@CurrentDate)/7  +1
SET @CurrentDate = DATEADD(DAY, 7, @CurrentDate); 
END

【讨论】:

    【解决方案2】:

    请将此用于具有周开始日期和结束日期的连击结果

    DECLARE @StartDate   datetime, 
            @EndDate     datetime, 
            @CurrentDate datetime, 
            @IsTrue      BIT=0 
    
    SET @StartDate = '2019-11-01' 
    SET @EndDate = '2019-12-14' 
    SET @CurrentDate=@StartDate 
    
    WHILE ( @CurrentDate <= @EndDate ) 
      BEGIN 
          IF( Format(@CurrentDate, 'MM') != Format(@StartDate, 'MM') 
              AND @IsTrue = 0 ) 
            BEGIN 
                SET @CurrentDate=Dateadd(month, Datediff(month, 0, @CurrentDate), 0) 
    
                PRINT @CurrentDate 
    
                SET @IsTrue=1 
            END 
    
          SELECT CONVERT(VARCHAR(20), ( Datepart(week, @CurrentDate) - 
                                        Datepart(week, 
                                        Dateadd(day, 1, Eomonth( 
                                        @CurrentDate, -1))) ) + 1), 
                 @CurrentDate, 
                 Dateadd(day, 6, @CurrentDate) 
    
          PRINT( CONVERT(VARCHAR(20), ( Datepart(week, @CurrentDate) - 
                                        Datepart(week, 
                                        Dateadd(day, 1, Eomonth( 
                                        @CurrentDate, -1))) ) + 1) ) 
    
          SET @CurrentDate = Dateadd(day, 7, @CurrentDate); 
      END 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多