LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 技巧:如何确定日期时间范围的日周月季度半年

admin
2024年7月20日 9:58 本文热度 486

不久前,我需要编写一个显示当前周开始和结束日期的方法。这让我开始思考其他时间段,如天、月、季度和年。以及如何找到它们的开始和结束日期。

因此,由于我喜欢来自我的开发人员同事的验证,我想与您分享。

Days & Years
日子和年份很容易处理,因为它们有明确的开始和结束。

public static DateTime StartOfDay(this DateTime date)   

{   

   return date.Date;   

}   

  

public static DateTime EndOfDay(this DateTime date)   

{   

   return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);   

}

要找到一天的开始,我们只需要返回 DateTime 的 Date 属性,这将为我们提供时间设置为 00:00:00 的日期。

对于时间结束时,我们创建一个具有相同日期的新 DateTime 对象,但将时间设置为 23:59:59。

public static DateTime StartOfYear(this DateTime date)   

{   

  return new DateTime(date.Year, 1, 1);   

}   

  

public static DateTime EndOfYear(this DateTime date)   

{   

  return new DateTime(date.Year, 12, 31, 23, 59, 59);   

}

对于一年的开始,我们创建一个具有相同年份的新 DateTime 对象,但首先将月份和日期设置为 1 月。
对于一年的年底,我们创建一个具有相同年份的新 DateTime 对象,但将月份和日期设置为 12 月 31 日,并将时间设置为 23:59:59。

Week

public static DateTime StartOfWeek(this DateTime date,  

 DayOfWeek startOfWeekDay = DayOfWeek.Sunday)   

{   

  while (date.DayOfWeek != startOfWeekDay)   

  {   

      date = date.AddDays(-1);   

  }   

  return date;   

}   

  

public static DateTime EndOfWeek(this DateTime date,  

 DayOfWeek startOfWeekDay = DayOfWeek.Sunday)   

{   

  return date.StartOfWeek(startOfWeekDay).AddDays(7).AddSeconds(-1);   

}

几周来,我们需要考虑一些我们大多数人认为理所当然的事情:我们什么时候开始一周?
在美国和欧洲,工作周从星期一开始,但在世界其他地区(如以色列),工作周从星期日开始。因此,我们需要有一个选项来处理这两种情况,通过使用 DayOfWeek startOfWeekDay 参数来判断一周的第一天是什么。
为了找到一周的开始,我们从当前日期开始,一次返回一天,直到我们到达一周开始的那一天。
要找到一周的结束,我们首先找到一周的开始,然后加上 7 天(到下周的开始),然后回溯 1 秒(到本周的最后一秒)。

Months

public static DateTime StartOfMonth(this DateTime date)   

{   

  return new DateTime(date.Year, date.Month, 1);   

}   

  

public static DateTime EndOfMonth(this DateTime date)   

{   

  return new DateTime(date.Year, date.Month,   

             DateTime.DaysInMonth(date.Year, date.Month), 23, 59, 59);   

}

对于月初,我们创建一个新的 DateTime 对象,该对象具有相同的年份和月份,但将日期设置为 1。
月底有点棘手,因为并非所有月份都有相同的天数,但 DateTime 为我们提供了 DaysInMonth 方法。因此,我们创建一个具有相同年份和月份的新 DateTime 对象,但将月份和日期设置为 _DateTime.DaysInMonth(date.年、日。月)。_我们将时间设置为 23:59:59。

Quarters

public static int QuarterOfTheYear(this DateTime date)   

{   

  return (int)Math.Ceiling((double)date.Month / 3);   

}   

  

public static DateTime StartOfQuarter(this DateTime date)   

{   

  var finalMonthOfQuarter= date.QuarterOfTheYear() * 3;   

  return new DateTime(date.Year, finalMonthOfQuarter— 2, 1);   

}   

  

public static DateTime EndOfQuarter(this DateTime date)   

{   

  var finalMonthOfQuarter = date.QuarterOfTheYear() * 3;   

  return new DateTime(date.Year, finalMonthOfQuatr,   

          DateTime.DaysInMonth(date.Year, finalMonthOfQuarter), 23, 59, 59);   

}

对于季度,我首先需要一种方法来告诉我们我们处于一年中的哪个季度,我们通过将月份除以 3 并使用 Math.Ceiling 四舍五入到最接近的整数来实现这一点。例如:如果我们在 2 月份,我们做 2/3 并得到 0.66,我们四舍五入到 1,看到 2 月份在第一季度。如果我们在 8 月份,我们做 8/3 并得到 2.66,我们将其四舍五入为 3,看到 8 月处于第 3 季度。

要找到季度的开始,我们只需要找到第一个月,我们通过找到最后一个月(季度数乘以 3 )然后回到 2 个月来做到这一点。假设我们处于最后一个季度,4*3 让我们到达 12 月(第 12 个月),如果我们回到 2 个月,我们将到达 10 月。
为了找到季度末,我们使用类似的技术,并将其与我们过去查找一个月天数的方法相结合。


该文章在 2024/7/22 12:10:37 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved