博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
日期和时间处理的类库
阅读量:6529 次
发布时间:2019-06-24

本文共 6643 字,大约阅读时间需要 22 分钟。

  发一个专门处理时间和日期的类库,记录以备查询。

using System; namespace Utilities {
/// /// Common DateTime Methods. /// /// public enum Quarter {
First = 1, Second = 2, Third = 3, Fourth = 4 } public enum Month {
January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12 } public class DateUtilities {
#region Quarter public static DateTime GetStartOfQuarter( int Year, Quarter Qtr ) {
if( Qtr == Quarter.First ) // 1st Quarter = January 1 to March 31 return new DateTime( Year, 1, 1, 0, 0, 0, 0 ); else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30 return new DateTime( Year, 4, 1, 0, 0, 0, 0 ); else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30 return new DateTime( Year, 7, 1, 0, 0, 0, 0 ); else // 4th Quarter = October 1 to December 31 return new DateTime( Year, 10, 1, 0, 0, 0, 0 ); } public static DateTime GetEndOfQuarter( int Year, Quarter Qtr ) {
if( Qtr == Quarter.First ) // 1st Quarter = January 1 to March 31 return new DateTime( Year, 3, DateTime.DaysInMonth( Year, 3 ), 23, 59, 59, 999 ); else if( Qtr == Quarter.Second ) // 2nd Quarter = April 1 to June 30 return new DateTime( Year, 6, DateTime.DaysInMonth( Year, 6 ), 23, 59, 59, 999 ); else if( Qtr == Quarter.Third ) // 3rd Quarter = July 1 to September 30 return new DateTime( Year, 9, DateTime.DaysInMonth( Year, 9 ), 23, 59, 59, 999 ); else // 4th Quarter = October 1 to December 31 return new DateTime( Year, 12, DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 ); } public static Quarter GetQuarter( Month month ) {
if( month <= Month.March ) // 1st Quarter = January 1 to March 31 return Quarter.First; else if( ( month >= Month.April ) && ( month <= Month.June ) ) // 2nd Quarter = April 1 to June 30 return Quarter.Second; else if( ( month >= Month.July ) && ( month <= Month.September ) ) // 3rd Quarter = July 1 to September 30 return Quarter.Third; else // 4th Quarter = October 1 to December 31 return Quarter.Fourth; } public static DateTime GetEndOfLastQuarter() { if( DateTime.Now.Month <= (int)Month.March ) //go to last quarter of previous year return GetEndOfQuarter( DateTime.Now.Year - 1, GetQuarter( Month.December )); else //return last quarter of current year return GetEndOfQuarter( DateTime.Now.Year, GetQuarter( (Month)DateTime.Now.Month)); } public static DateTime GetStartOfLastQuarter() {
if( DateTime.Now.Month <= 3 ) //go to last quarter of previous year return GetStartOfQuarter( DateTime.Now.Year - 1, GetQuarter( Month.December )); else //return last quarter of current year return GetStartOfQuarter( DateTime.Now.Year, GetQuarter( (Month)DateTime.Now.Month)); } public static DateTime GetStartOfCurrentQuarter() {
return GetStartOfQuarter( DateTime.Now.Year, GetQuarter( (Month)DateTime.Now.Month )); } public static DateTime GetEndOfCurrentQuarter() {
return GetEndOfQuarter( DateTime.Now.Year, GetQuarter( (Month)DateTime.Now.Month )); } #endregion #region Weeks public static DateTime GetStartOfLastWeek() {
int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7; DateTime dt = DateTime.Now.Subtract( System.TimeSpan.FromDays( DaysToSubtract ) ); return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 ); } public static DateTime GetEndOfLastWeek() {
DateTime dt = GetStartOfLastWeek().AddDays(6); return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 ); } public static DateTime GetStartOfCurrentWeek() {
int DaysToSubtract = (int)DateTime.Now.DayOfWeek ; DateTime dt = DateTime.Now.Subtract( System.TimeSpan.FromDays( DaysToSubtract ) ); return new DateTime( dt.Year, dt.Month, dt.Day, 0, 0, 0, 0 ); } public static DateTime GetEndOfCurrentWeek() {
DateTime dt = GetStartOfCurrentWeek().AddDays(6); return new DateTime( dt.Year, dt.Month, dt.Day, 23, 59, 59, 999 ); } #endregion #region Months public static DateTime GetStartOfMonth( int Month, int Year ) {
return new DateTime( Year, Month, 1, 0, 0, 0, 0 ); } public static DateTime GetEndOfMonth( int Month, int Year ) {
return new DateTime( Year, Month, DateTime.DaysInMonth( Year, Month ), 23, 59, 59, 999 ); } public static DateTime GetStartOfLastMonth() {
if( DateTime.Now.Month == 1 ) return GetStartOfMonth( 12, DateTime.Now.Year - 1); else return GetStartOfMonth( DateTime.Now.Month -1, DateTime.Now.Year ); } public static DateTime GetEndOfLastMonth() {
if( DateTime.Now.Month == 1 ) return GetEndOfMonth( 12, DateTime.Now.Year - 1); else return GetEndOfMonth( DateTime.Now.Month -1, DateTime.Now.Year ); } public static DateTime GetStartOfCurrentMonth() {
return GetStartOfMonth( DateTime.Now.Month, DateTime.Now.Year ); } public static DateTime GetEndOfCurrentMonth() {
return GetEndOfMonth( DateTime.Now.Month, DateTime.Now.Year ); } #endregion #region Years public static DateTime GetStartOfYear( int Year ) {
return new DateTime( Year, 1, 1, 0, 0, 0, 0 ); } public static DateTime GetEndOfYear( int Year ) {
return new DateTime( Year, 12, DateTime.DaysInMonth( Year, 12 ), 23, 59, 59, 999 ); } public static DateTime GetStartOfLastYear() {
return GetStartOfYear( DateTime.Now.Year - 1 ); } public static DateTime GetEndOfLastYear() {
return GetEndOfYear( DateTime.Now.Year - 1 ); } public static DateTime GetStartOfCurrentYear() {
return GetStartOfYear( DateTime.Now.Year ); } public static DateTime GetEndOfCurrentYear() {
return GetEndOfYear( DateTime.Now.Year ); } #endregion #region Days public static DateTime GetStartOfDay( DateTime date ) {
return new DateTime( date.Year, date.Month, date.Day, 0, 0, 0, 0 ); } public static DateTime GetEndOfDay( DateTime date ) {
return new DateTime( date.Year, date.Month, date.Day, 23, 59, 59, 999 ); } #endregion } }

 

转载地址:http://rttbo.baihongyu.com/

你可能感兴趣的文章
Android获取当前时间与星期几
查看>>
jenkins2 multibranch
查看>>
Css定位-定位
查看>>
sort,uniq命令
查看>>
am335x 电容屏驱动添加。
查看>>
JavaScript Unicode字符操作
查看>>
rhel-server-7.2-x86_64无法联网(VMware环境)
查看>>
Nginx配置中的log_format用法梳理(设置详细的日志格式)
查看>>
Atitit 软件工程概览attilax总结
查看>>
优化LibreOffice如此简单
查看>>
【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
查看>>
秒杀系统设计方案
查看>>
3D印花芭蕾舞鞋为舞者科学地保护双脚
查看>>
冲浪科技获Ventech China数百万美元天使轮融资,发力自动驾驶行业
查看>>
通过ActionTrail监控AccessKey的使用
查看>>
从 JavaScript 到 TypeScript
查看>>
一个mysql复制中断的案例
查看>>
【最佳实践】OSS开源工具ossutil-大文件断点续传
查看>>
Linux常用的服务器构建
查看>>
深入了解 Weex
查看>>