C#获取本周、上周、本月、上月、本季度、上季度、本年、上一年起始时间和结束时间

  /// 取得某月的第一天

  1. /// </summary>
  2. /// <param name="datetime">要取得月份第一天的时间</param>
  3. /// <returns></returns>
  4. private DateTime FirstDayOfMonth(DateTime datetime)
  5. {
  6. return datetime.AddDays(1 - datetime.Day);
  7. }
  8. /**//// <summary>
  9. /// 取得某月的最后一天
  10. /// </summary>
  11. /// <param name="datetime">要取得月份最后一天的时间</param>
  12. /// <returns></returns>
  13. private DateTime LastDayOfMonth(DateTime datetime)
  14. return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
  15. }
  16. /**//// <summary>
  17. /// 取得上个月第一天
  18. /// </summary>
  19. /// <param name="datetime">要取得上个月第一天的当前时间</param>
  20. /// <returns></returns>
  21. private DateTime FirstDayOfPreviousMonth(DateTime datetime)
  22. {
  23. return datetime.AddDays(1 - datetime.Day).AddMonths(-1);
  24. }
  25. /**//// <summary>
  26. /// 取得上个月的最后一天
  27. /// </summary>
  28. /// <param name="datetime">要取得上个月最后一天的当前时间</param>
  29. /// <returns></returns>
  30. private DateTime LastDayOfPrdviousMonth(DateTime datetime)
  31. {
  32. return datetime.AddDays(1 - datetime.Day).AddDays(-1);
  33. }

C# DateTime 日期加1天 减一天 加一月 减一月 等方法

    1. //今天
    2. DateTime.Now.Date.ToShortDateString();
    3. //昨天,就是今天的日期减一
    4. DateTime.Now.AddDays(-1).ToShortDateString();
    5. //明天,同理,加一
    6. DateTime.Now.AddDays(1).ToShortDateString();
    7. //本周(要知道本周的第一天就得先知道今天是星期几,从而得知本周的第一天就是几天前的那一天,要注意的是这里的每一周是从周日始至周六止
    8. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
    9. DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
    10. //如果你还不明白,再看一下中文显示星期几的方法就应该懂了
    11. //由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的
    12. string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
    13. Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
    14. //上周,同理,一个周是7天,上周就是本周再减去7天,下周也是一样
    15. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
    16. DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
    17. //下周
    18. DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
    19. DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
    20. //本月,很多人都会说本月的第一天嘛肯定是1号,最后一天就是下个月一号再减一天。当然这是对的
    21. //一般的写法
    22. DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
    23. DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天
    24. //巧用C#里ToString的字符格式化更简便
    25. DateTime.Now.ToString("yyyy-MM-01");
    26. DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();
    27. //上个月,减去一个月份
    28. DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
    29. DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
    30. //下个月,加去一个月份
    31. DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();
    32. DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();
    33. //7天后
    34. DateTime.Now.Date.ToShortDateString();
    35. DateTime.Now.AddDays(7).ToShortDateString();
    36. //7天前
    37. DateTime.Now.AddDays(-7).ToShortDateString();
    38. DateTime.Now.Date.ToShortDateString();
    39. //本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天
    40. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();
    41. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();
    42. //上年度,不用再解释了吧
    43. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();
    44. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();
    45. //下年度
    46. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();
    47. DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
    48. //本季度,很多人都会觉得这里难点,需要写个长长的过程来判断。其实不用的,我们都知道一年四个季度,一个季度三个月
    49. //首先我们先把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了
    50. DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
    51. //同理,本季度的最后一天就是下季度的第一天减一
    52. DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
    53. //下季度,相信你们都知道了。。。。收工
    54. DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
    55. DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
    56. //上季度
    57. DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");
    58. DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
    59. c# 根据日期获取星期方法

      protected void Page_Load(object sender, EventArgs e)
          {
              int m = System.DateTime.Today.Month;
              int y = System.DateTime.Today.Year;
              int d = System.DateTime.Today.Day;
              int weeks = getWeekDay(y, m, d);
              switch (weeks)
              { 
                  case 1:
                      this.TextBox1.Text = "星期一";
                      break;
                  case 2:
                      this.TextBox1.Text = "星期二";
                      break;
                  case 3:
                      this.TextBox1.Text = "星期三";
                      break;
                  case 4:
                      this.TextBox1.Text = "星期四";
                      break;
                  case 5:
                      this.TextBox1.Text = "星期五";
                      break;
                  case 6:
                      this.TextBox1.Text = "星期六";
                      break;
                  case 7:
                      this.TextBox1.Text = "星期日";
                      break;
              }
              
              
          }

      /// <summary>根据日期,获得星期几</summary>
          /// <param name="y">年</param> 
          /// <param name="m">月</param> 
          /// <param name="d">日</param> 
          /// <returns>星期几,1代表星期一;7代表星期日</returns>
          public static int getWeekDay(int y, int m, int d)
          {
              if (m == 1) m = 13;
              if (m == 2) m = 14;
              int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
              return week;
          }

    60. C# 根据年、月、周、星期获得日期等

      /// <summary> 
      /// 取指定日期是一年中的第几周 
      /// </summary> 
      /// <param name="dtime">给定的日期</param> 
      /// <returns>数字 一年中的第几周</returns> 
      public static int WeekOfYear(DateTime dtime)
      {
          try
          {
              //确定此时间在一年中的位置
              int dayOfYear = dtime.DayOfYear;
              //当年第一天
              DateTime tempDate = new DateTime(dtime.Year, 1, 1);
              //确定当年第一天
              int tempDayOfWeek = (int)tempDate.DayOfWeek;
              tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
              ////确定星期几
              int index = (int)dtime.DayOfWeek;
              index = index == 0 ? 7 : index;
       
              //当前周的范围
              DateTime retStartDay = dtime.AddDays(-(index - 1));
              DateTime retEndDay = dtime.AddDays(6 - index);
       
              //确定当前是第几周
              int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
       
              if (retStartDay.Year < retEndDay.Year)
              {
                  weekIndex = 1;
              }
       
              return weekIndex;
          }
          catch (Exception ex)
          {
              throw new Exception(ex.Message);
          }
       
      }
       
      /// <summary>
      /// 求某年有多少周
      /// </summary>
      /// <param name="dtime"></param>
      /// <returns></returns>
      public static int GetWeekOfYear(DateTime dtime)
      {
          int countDay = DateTime.Parse(dtime.Year + "-12-31").DayOfYear;
          int countWeek = countDay / 7;
          return countWeek;
      }
       
       
      //根据年月日获得星期几
      public static string CaculateWeekDay(int month, int day)
      {
          string weekstr = string.Empty;
          int year = DateTime.Now.Year;
          //把一月和二月看成是上一年的十三月和十四月
          if (month == 1) { month = 13; year--; }
          if (month == 2) { month = 14; year--; }
          int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
          switch (week)
          {
              case 0: weekstr = "1"break;
              case 1: weekstr = "2"break;
              case 2: weekstr = "3"break;
              case 3: weekstr = "4"break;
              case 4: weekstr = "5"break;
              case 5: weekstr = "6"break;
              case 6: weekstr = "7"break;
          }
          return weekstr;
      }
       
      /// <summary>
      /// 根据2个时间段获得相应的周数
      /// </summary>
      /// <param name="beginDate"></param>
      /// <param name="endDate"></param>
      /// <returns></returns>
      public static int WeekOfDate(DateTime beginDate, DateTime endDate)
      {
          TimeSpan ts1 = new TimeSpan(beginDate.Ticks);
          TimeSpan ts2 = new TimeSpan(endDate.Ticks);
          TimeSpan ts = ts2.Subtract(ts1).Duration();
          int weeks = ts.Days / 7;
       
          //确定此时间在一年中的位置
          int dayOfYear = beginDate.DayOfYear;
          //当年第一天
          DateTime tempDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
          //最后一天
          DateTime tempendDate = new DateTime(endDate.Year, endDate.Month, endDate.Day);
          int tempDayOfWeek = (int)tempDate.DayOfWeek;
          tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
       
          ////确定星期几
          int startindex = (int)beginDate.DayOfWeek;
          startindex = startindex == 0 ? 7 : startindex;
          //当前周的范围
          DateTime retStartDay = beginDate.AddDays(-(startindex - 1));
          DateTime retEndDay = beginDate.AddDays(7 - startindex);
          //确定当前是第几周
          int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
       
          return weeks;
      }
       
      /// <summary>
      /// 根据起始时间,获取第几周
      /// </summary>
      /// <param name="dtime">当前时间</param>
      /// <returns></returns>
      public static int WeekOfTermDate(DateTime dtime)
      {          
          string datetime = "2011-3-1";
       
          TimeSpan ts1 = new TimeSpan(dtime.Ticks);
          TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime).Ticks);
          TimeSpan ts = ts2.Subtract(ts1).Duration();
       
          //确定此时间在一年中的位置
          int dayOfYear = ts.Days;
          //当年第一天
          DateTime tempDate = new DateTime(Convert.ToDateTime(datetime).Year, Convert.ToDateTime(datetime).Month, Convert.ToDateTime(datetime).Day);
           
          int tempDayOfWeek = (int)tempDate.DayOfWeek;
          tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
          ////确定星期几
          int index = (int)dtime.DayOfWeek;
          index = index == 0 ? 7 : index;
       
          //当前周的范围
          DateTime retStartDay = dtime.AddDays(-(index - 1));
          DateTime retEndDay = dtime.AddDays(7 - index);
       
          //确定当前是第几周
          int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);          
          return weekIndex;
      }
       
      /// <summary>
      /// 根据周,星期获得具体年月日
      /// </summary>
      /// <param name="week">第几周</param>
      /// <param name="day">星期几</param>
      /// <returns></returns>
      public static DateTime DateTimeByWeekAndDay(int week, int day)
      {           
          DateTime someTime = Convert.ToDateTime("2011-3-1");
       
          int i = someTime.DayOfWeek - DayOfWeek.Monday;  
          if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
          TimeSpan ts = new TimeSpan(i, 0, 0, 0);          
       
          //获取第N周的星期一
          someTime = someTime.Subtract(ts).AddDays((week - 1) * 7);
          //获得星期几
          someTime = someTime.AddDays(day - 1);
          return someTime;
      }

      private static int GetWeekOfYear()
         {
                  //一.找到第一周的最后一天(先获取1月1日是星期几,从而得知第一周周末是几)
                  int firstWeekend=7-Convert.ToInt32(DateTime.Parse(DateTime.Today.Year + "-1-1").DayOfWeek);
                  
                  //二.获取今天是一年当中的第几天
                  int currentDay = DateTime.Today.DayOfYear;

      //三.(今天 减去 第一周周末)/7 等于 距第一周有多少周 再加上第一周的1 就是今天是今年的第几周了
                  //    刚好考虑了惟一的特殊情况就是,今天刚好在第一周内,那么距第一周就是0 再加上第一周的1 最后还是1
                  return Convert.ToInt32(Math.Ceiling((currentDay - firstWeekend) / 7.0)) + 1;
           }

上一篇:react+redux+generation-modation脚手架添加一个todolist


下一篇:基于Linux 的VM TOOLS Install