【问题标题】:Date range from weeknumber + year日期范围从周数 + 年
【发布时间】:2009-03-29 11:35:44
【问题描述】:

我有 2 个下拉列表 1 带有周数 1 带有年份。 我想从这些数据中提取日期范围。

所以:

2009 年第 13 周将给出:

2009 年 3 月 23 日星期一 2009 年 3 月 24 日,星期二 ...

VB.Net 首选,但 C# 解决方案也可以。

编辑: 好的,我想我应该提到这是针对欧洲日期的。

【问题讨论】:

    标签: .net date


    【解决方案1】:
    CultureInfo curCulture = CultureInfo.CurrentCulture;
    
    DateTime targetDate = curCulture.Calendar.AddWeeks(new DateTime([year], 1, 1), [Week]);
    
    DayOfWeek targetWeekDay =
        curCulture.Calendar.GetDayOfWeek(targetDate);
    
    DateTime targetBeginningOfWeek = targetDate.AddDays(-1*Convert.ToInt16(targetWeekDay));
    

    targetBeginningOfWeek 将包含该周的第一天,加上 7 天并获得该周的剩余时间

    【讨论】:

      【解决方案2】:

      尝试使用以下功能:

      Public Function Week2Date1(ByVal Week2Date2 As Date) As Date
          Week2Date1 = DateAdd(DateInterval.Day, -4, Week2Date2)
      
      End Function
      
      Public Function Week2Date2(ByVal WeekNo As Integer) As Date
          Week2Date2 = DateSerial(Now.Year, 1, (WeekNo) * 7)
      
      End Function
      

      我正在使用这些来确定每周的星期一和星期五的日期。

      地点:

      • 函数 Week2Date2 将返回一周中的星期五编号和
      • 函数 Week2Date1 将根据 Week2Date2 返回的日期值返回周数的星期一。

      【讨论】:

        【解决方案3】:

        以下代码从周数和年份获取日期范围。但它是用 Java 编写的。
        希望对您有所帮助。

           System.out.println("date Range from weekNumber and year but in Java");
           System.out.println(); // print a blank line
        
           // get the input from the user
           Scanner sc = new Scanner(System.in);
        
           System.out.print("Enter the week : ");
           int weekNumber  = sc.nextInt(); 
           System.out.print("Enter the Year: ");
           int year = sc.nextInt() ;
        
        
        
           Calendar cal = Calendar.getInstance();
           //cal.setTime(new Date());
        
           cal.set(Calendar.YEAR, year);
           cal.set(Calendar.WEEK_OF_YEAR, weekNumber);
        
           SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        
           cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
           System.out.println(formatter.format(cal.getTime())); // start date
        
           cal.add(Calendar.DAY_OF_WEEK, 6);
           System.out.println(formatter.format(cal.getTime())); // end date
        

        【讨论】:

          【解决方案4】:
          var date = DateTime.MinValue + 2009.Years() + 13.Weeks();
          

          通过在 Codeplex 上使用 Fluent DateTime 项目。

          【讨论】:

            猜你喜欢
            • 2021-08-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-13
            • 2015-07-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多