C# 获取当前年份的周期及周期所在日期范围(推荐)
作者:有创软件工作室 时间:2021-10-06 15:00:44
最近有一个项目要用到年份周期,用于数据统计图表展示使用,当中用到年份周期,以及年份周期所在的日期范围。当初设想通过已知数据来换算年份周期,经过搜索资料发现通过数据库SQL语句来做,反而更加复杂。现在改变思路通过C#后台代码来算出两段日期范围中年份周期,在依据年份周期所对应的日期范围进行数据库查询进行统计。需要解决以下两个点问题,
第一点:依据日期查找所在年份的第几周;
第二点:依据年份所在的周期计算出周期所在的日期范围。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
GregorianCalendar gc = new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
Console.WriteLine("当前第{0}周", weekOfYear);
DateTime startDate, lastDate;
for (int i = 1; i <= 53; i++)
{
GetDaysOfWeeks(DateTime.Now.Year, i, out startDate, out lastDate);
Console.WriteLine("第{0}周", i);
Console.WriteLine(startDate);
Console.WriteLine(lastDate);
}
Console.ReadLine();
}
public static bool GetDaysOfWeeks(int year, int index, out DateTime first, out DateTime last)
{
first = DateTime.MinValue;
last = DateTime.MinValue;
if (year < 1700 || year > 9999)
{
//"年份超限"
return false;
}
if (index < 1 || index > 53)
{
//"周数错误"
return false;
}
DateTime startDay = new DateTime(year, 1, 1); //该年第一天
DateTime endDay = new DateTime(year + 1, 1, 1).AddMilliseconds(-1);
int dayOfWeek = 0;
if (Convert.ToInt32(startDay.DayOfWeek.ToString("d")) > 0)
dayOfWeek = Convert.ToInt32(startDay.DayOfWeek.ToString("d")); //该年第一天为星期几
if (dayOfWeek == 0) { dayOfWeek = 7; }
if (index == 1)
{
first = startDay.AddDays(7 - dayOfWeek - 6);
if (dayOfWeek == 6)
{
last = first;
}
else
{
last = startDay.AddDays((7 - dayOfWeek));
}
}
else
{
first = startDay.AddDays((8 - dayOfWeek) + (index - 2) * 7); //index周的起始日期
last = first.AddDays(6);
//if (last > endDay)
//{
// last = endDay;
//}
}
if (first > endDay) //startDayOfWeeks不在该年范围内
{
//"输入周数大于本年最大周数";
return false;
}
return true;
}
}
}
执行结果
总结
以上所述是小编给大家介绍的C# 获取当前年份的周期及周期所在日期范围(推荐),希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://www.cnblogs.com/xiaxiaoping/p/9028733.html
标签:C#,周期,年份周期
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java动态显示文件上传进度实现代码
2022-09-09 20:18:17
关于Java反编译字节码文件
2021-10-07 01:41:32
java创建多级目录文件的实例讲解
2023-11-27 09:29:02
Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用
2023-07-07 00:20:28
![](https://img.aspxhome.com/file/2023/3/63453_0s.png)
流读取导致StringBuilder.toString()乱码的问题及解决
2022-12-20 13:34:14
VS2019配置OpenCV时找不到Microsoft.Cpp.x64.user的解决方法
2023-07-01 01:57:36
![](https://img.aspxhome.com/file/2023/1/100301_0s.png)
SpringMvc框架的简介与执行流程详解
2022-10-15 18:49:00
![](https://img.aspxhome.com/file/2023/4/62354_0s.jpg)
jvm运行原理以及类加载器实例详解
2023-10-23 18:39:03
![](https://img.aspxhome.com/file/2023/9/66019_0s.png)
Java 实战图书管理系统的实现流程
2023-12-19 05:11:13
![](https://img.aspxhome.com/file/2023/1/64541_0s.png)
Java数据结构之栈与队列实例详解
2021-05-29 03:25:13
![](https://img.aspxhome.com/file/2023/1/61591_0s.png)
用Java实现简单ATM机功能
2023-05-10 13:36:03
Spring SpringMVC在启动完成后执行方法源码解析
2023-01-01 12:46:57
Spring框架应用的权限控制系统详解
2023-11-11 14:17:11
![](https://img.aspxhome.com/file/2023/2/59332_0s.png)
Java编程用两个栈实现队列代码分享
2023-03-22 01:05:05
IDEA 插件 mapper和xml互相跳转操作
2021-07-19 02:48:13
![](https://img.aspxhome.com/file/2023/4/66024_0s.jpg)
java实现摄像头截图功能
2023-12-01 19:53:22
Java WebService 简单实例(附实例代码)
2023-01-25 07:52:18
![](https://img.aspxhome.com/file/2023/8/64528_0s.png)
Java使用application.property读取文件里面的值
2023-11-01 07:24:56
Java continue break制作简单聊天室程序
2022-12-20 01:23:01
C#中动态显示当前系统时间的实例方法
2023-06-20 14:46:02