zoukankan      html  css  js  c++  java
  • 获取 当前时间 本周,本月,本季度,本年的起始时间

    因为项目中需要用时间作为条件进行统计,所以需要获取 当前时间 本周,本月,本季度,本年的起始时间,代码如下

     1 #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
     2         /// <summary>
     3         /// 统计时间类型
     4         /// </summary>
     5         public enum EnumTimeType
     6         {
     7             每周 = 0,
     8             每月 = 1,
     9             每季度 = 2,
    10             每年 = 3,
    11         }
    12         /// <summary>
    13         /// 获取结束时间
    14         /// </summary>
    15         /// <param name="TimeType">Week、Month、Season、Year</param>
    16         /// <param name="now"></param>
    17         /// <returns></returns>
    18         public static DateTime? GetTimeStartByType(EnumTimeType TimeType, DateTime now)
    19         {
    20             DateTime? returnTime = null;
    21             switch (TimeType)
    22             {
    23                 case EnumTimeType.每周:
    24                     if (now.DayOfWeek == 0)
    25                     {
    26                         returnTime = now.AddDays(-6);
    27                     }
    28                     else
    29                     {
    30                         returnTime = now.AddDays(-(int)now.DayOfWeek + 1);
    31                     }
    32 
    33                     break;
    34                 case EnumTimeType.每月:
    35                     returnTime = now.AddDays(-now.Day + 1);
    36                     break;
    37                 case EnumTimeType.每季度:
    38                     var time = now.AddMonths(0 - ((now.Month - 1) % 3));
    39                     returnTime = time.AddDays(-time.Day + 1);
    40                     break;
    41                 case EnumTimeType.每年:
    42                     returnTime = now.AddDays(-now.DayOfYear + 1);
    43                     break;
    44                 default:
    45                     return null;
    46             }
    47             return DateTime.Parse(returnTime.Value.ToString("yyyy/MM/dd 00:00:00"));
    48         }
    49 
    50         /// <summary>
    51         /// 获取结束时间
    52         /// </summary>
    53         /// <param name="TimeType">Week、Month、Season、Year</param>
    54         /// <param name="now"></param>
    55         /// <returns></returns>
    56         public static DateTime? GetTimeEndByType(EnumTimeType TimeType, DateTime now)
    57         {
    58             DateTime? returnTime = null;
    59             switch (TimeType)
    60             {
    61                 case EnumTimeType.每周:
    62                     if (now.DayOfWeek == 0)
    63                     {
    64                         returnTime = now;
    65                     }
    66                     else
    67                     {
    68                         returnTime = now.AddDays(7 - (int)now.DayOfWeek); 
    69             }
    70             break;
    71         case EnumTimeType.每月:
    72             returnTime = now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
    73             break;
    74         case EnumTimeType.每季度:
    75             var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
    76             returnTime = time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
    77             break;
    78         case EnumTimeType.每年:
    79             var time2 = now.AddYears(1);
    80             returnTime = time2.AddDays(-time2.DayOfYear);
    81             break;
    82         default:
    83             return null;
    84     }
    85     return DateTime.Parse(returnTime.Value.ToString("yyyy/MM/dd 23:59:59"));
    86 }
    87 #endregion 

    调用如下

     1 void Main()
     2 {
     3     GetTimeStartByType(EnumTimeType.每周,DateTime.Now).Dump();
     4     GetTimeEndByType(EnumTimeType.每周, DateTime.Now).Dump();
     5     "------------------------本月------------------------".Dump();
     6     GetTimeStartByType(EnumTimeType.每月, DateTime.Now).Dump();
     7     GetTimeEndByType(EnumTimeType.每月, DateTime.Now).Dump();
     8     "------------------------本季度----------------------".Dump();
     9     GetTimeStartByType(EnumTimeType.每季度, DateTime.Now).Dump();
    10     GetTimeEndByType(EnumTimeType.每季度, DateTime.Now).Dump();
    11     "------------------------本年------------------------".Dump();
    12     GetTimeStartByType(EnumTimeType.每年, DateTime.Now).Dump();
    13     GetTimeEndByType(EnumTimeType.每年, DateTime.Now).Dump();
    14     "------------------今天是本年第几周------- ----------".Dump();
    15     ((DateTime.Now.DayOfYear/7+1)+"").ToString().Dump();
    16 }

    显示结果

    总结,计算这些时间间隔用到了微软提供的DayOfWeek和DayOfYear,真的是很方便了。

    其中周比较 特殊一点,因为DayOfWeek获取星期天的时候值为0,所以需要注意下一。

    需要的人可自行获取。

  • 相关阅读:
    (转)要在自己感兴趣的领域成为专家,要经过一万小时训练
    (学习日记)数据结构第一章中life游戏开发的学习记录
    (转)响应式Web设计是大势所趋还是时代的产物
    (学习日记)裘宗燕:C/C++ 语言中的表达式求值
    (转)如果你喜欢编程 给想做程序员的人的7个建议
    NHibernate 中删除数据的几种方法
    如何避免在安装SQL SERVER 2008时,出现Rule “Previous releases of Microsoft Visual Studio 2008″ failed.
    【转】A brief overview of Ncqrs
    爱博图微博图片批量下载小工具
    解释用Q号算出你的年龄的“奥妙”。。。
  • 原文地址:https://www.cnblogs.com/widows/p/10895360.html
Copyright © 2011-2022 走看看