C#如何获取指定周的日期范围

1. 不允许跨年

1) 第一周的第一天从每年的第一天开始,最后一周的最后一天为每年的最后一天。

 1 static void Main(string[] args)
 2 {
 3     DateTime first, last;
 4     int[] years = new int[] { 2015, 2016, 2017, 2018 };
 5     int[] weeks = new int[] { 1, 52, 53 };
 6     foreach (int y in years)
 7     {
 8         foreach (int w in weeks)
 9         {
10             bool result = CalcWeekDay(y, w, out first, out last);
11             Console.WriteLine("{0}第{1}周({2:yyyy-MM-dd} ~ {3:yyyy-MM-dd}) --{4}", y, w, first, last, result);
12         }
13         Console.WriteLine();
14     }
15 }
16
17 public static bool CalcWeekDay(int year, int week, out DateTime first, out DateTime last)
18 {
19     first = DateTime.MinValue;
20     last = DateTime.MinValue;
21     //年份超限
22     if (year < 1700 || year > 9999) return false;
23     //周数错误
24     if (week < 1 || week > 53) return false;
25     //指定年范围
26     DateTime start = new DateTime(year, 1, 1);
27     DateTime end = new DateTime(year, 12, 31);
28     int startWeekDay = (int)start.DayOfWeek;
29
30     if (week == 1)
31     {
32         first = start;
33         last = start.AddDays(6 - startWeekDay);
34     }
35     else
36     {
37         //周的起始日期
38         first = start.AddDays((7 - startWeekDay) + (week - 2) * 7);
39         last = first.AddDays(6);
40         if (last > end)
41         {
42             last = end;
43         }
44     }
45     return (first <= end);  

2) 程序执行结果

2. 允许跨年

1) 每年的尾周剩余天数计入下一年第一周。

 1 public static bool CalcWeekDay(int year, int week, out DateTime first, out DateTime last)
 2 {
 3     first = DateTime.MinValue;
 4     last = DateTime.MinValue;
 5     //年份超限
 6     if (year < 1700 || year > 9999) return false;
 7     //周数错误
 8     if (week < 1 || week > 53) return false;
 9     //指定年范围
10     DateTime start = new DateTime(year, 1, 1);
11     DateTime end = new DateTime(year, 12, 31);
12     int startWeekDay = (int)start.DayOfWeek;
13     //周的起始日期
14     first = start.AddDays((7 - startWeekDay) + (week - 2) * 7);
15     last = first.AddDays(6);
16     //结束日期跨年
17     return (last <= end);
18 }  

2) 程序执行结果
 

时间: 2024-08-05 19:37:05

C#如何获取指定周的日期范围的相关文章

js中使用eval()方法将字符串转换成日期格式、并获取指定时间的日期

1.在js中eval()方法将字符串格式数据转换成日期格式 function getDate(strDate) {         //strDate为需要转换成日期格式的字符串         var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,                 function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');    

PHP获取一周的日期

function get_week($time = '', $format='Y-m-d'){ $time = $time != '' ? $time : time(); //获取当前周几 $week = date('w', $time); $weekname = array('周一','周二','周三','周四','周五','周六','周日'); //星期日排到末位 if(empty($week)){ $week=7; } $date = []; for ($i=0; $i<7; $i++){

js日期计算及快速获取周、月、季度起止日,获取指定日期周数以及星期几的小例子

JS获取日期时遇到如下需求,根据某年某周获取一周的日期.如开始日期规定为星期四到下一周的星期五为一周. 格式化日期: function getNowFormatDate(theDate) { var day = theDate; var Year = 0; var Month = 0; var Day = 0; var CurrentDate = ""; // 初始化时间 Year= day.getFullYear();// ie火狐下都可以 Month= day.getMonth()

获取指定日期之间的各个周和月

日志格式化类 Date.class.php <?php class Datefmt{     function __construct() {}     /**      * 根据指定日期获取所在周的起始时间和结束时间      */     public function get_weekinfo_by_date($date) {         $idx = strftime("%u", strtotime($date));         $mon_idx = $idx -

获取指定日期的常用前后节点(月初月末周一周末等等)

原文:获取指定日期的常用前后节点(月初月末周一周末等等) 注:周节点方面是根据中国习惯,视周一为起,周日为末. /*--------------------------------- 函数:获取某日期的特定起止节点v0.01 Author:AhDung Update:201305151755 ---------------------------------*/ ALTER FUNCTION dbo.FGetSpecialDate_ahdung(@date DATE, @SpcDate VARC

Java中获取指定日为星期几及其他日期操作

摘自:http://blog.csdn.net/cselmu9/article/details/8625530#t6 在开发中经常会使用到一些日期方面的操作,下面例子展示几个常用的操作. 1.取得指定日期是星期几 取得指定日期是星期几可以采用下面两种方式取得日期是星期几: a.使用Calendar类 1 //根据日期取得星期几 2 public static String getWeek(Date date){ 3 String[] weeks = {"星期日","星期一&q

获取当前周、上一周、下一周的日期

使用JS或者是JQuery获取当前周和上一周.下一周的日期时间信息的方法,摘自 http://www.qdfuns.com/notes/18184/435de54dd1e41baf6777039e394c0100.html,目的仅仅是为了做个笔记,侵删!以下是代码以及解析: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html > <head> <meta h

获取指定月 获取指定日期是星期几

public static List<ExcelDateHead> getByMonth(String date){ DateFormat dateFormat=new SimpleDateFormat("yyyy-MM"); Calendar c = Calendar.getInstance(); try { c.setTime(dateFormat.parse(date)); } catch (ParseException e) { // TODO: handle ex

Java 获取指定日期的方法总结 -转

格式化日期 String-->Date  或者 Data-->String SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    Date  date = sdf.parse("2009-11-04");//String-->Date String  sdate  = sdf.format(date );// Data-->String ==================