zoukankan      html  css  js  c++  java
  • 日期算法公式

    注:一下方法的参数都为本地系统时间,即:datetime.now

    /// <summary>
    /// 计算本周起始日期(礼拜一的日期)
    /// </summary>
    /// <param name="someDate">该周中任意一天</param>
    /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
    public static DateTime CalculateFirstDateOfWeek(DateTime someDate)
    {
         int i = someDate.DayOfWeek - DayOfWeek.Monday;
         if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
         TimeSpan ts = new TimeSpan(i, 0, 0, 0);
         return someDate.Subtract(ts).Date;
    }

    /// <summary>
    /// 计算本周结束日期(礼拜五的日期)
    /// </summary>
    /// <param name="someDate">该周中任意一天</param>
    /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
    public static DateTime CalculateLastDateOfWeek(DateTime someDate)
    {
        int i = someDate.DayOfWeek - DayOfWeek.Sunday;
        if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。
        TimeSpan ts = new TimeSpan(i, 0, 0, 0);
        //return someDate.Add(ts).AddDays(1).Date;
        return someDate.Add(ts).AddDays(-2).AddHours(24).AddSeconds(-1).Date;
    }
    /// <summary>
    /// 判断选择的日期是否是本周(根据系统当前时间决定的‘本周'比较而言)
    /// </summary>
    /// <param name="someDate"></param>
    /// <returns></returns>
    public static bool IsThisWeek(DateTime someDate)
    {
         //得到someDate对应的周一
          DateTime someMon = CalculateFirstDateOfWeek(someDate);
         //得到本周一
        DateTime nowMon = CalculateFirstDateOfWeek(DateTime.Now);
        TimeSpan ts = someMon - nowMon;
        if (ts.Days < 0)
        ts = -ts;//取正
        if (ts.Days >= 7)
       {
         return false;
       }
       else
       {
         return true;
       }
       }

    /// <summary>
    /// 取得某月的第一天
    /// </summary>
    /// <param name="datetime">要取得月份第一天的时间</param>
    /// <returns></returns>
    public static DateTime FirstDayOfMonth(DateTime datetime)
    {
        return datetime.AddDays(1 - datetime.Day);
    }

    /// <summary>
    /// 取得某月的最后一天
    /// </summary>
    /// <param name="datetime">要取得月份最后一天的时间</param>
    /// <returns></returns>
    public static DateTime LastDayOfMonth(DateTime datetime)
    {
        return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
    }

    /// <summary>
    /// 取得学期第一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime FirstDayOfTerm(DateTime datetime)
    {
         if (datetime.Month >= 2 && datetime.Month <= 7)
       {
         return new DateTime(datetime.Year, 2, 1);
       }
        else if (datetime.Month < 2)
       {
         return new DateTime(datetime.Year - 1, 8, 1);
       }
       else
      {
        return new DateTime(datetime.Year, 8, 1);
      }
    }

    /// <summary>
    /// 取得学期最后一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime LastDayOfTerm(DateTime datetime)
    {
       return FirstDayOfTerm(datetime).AddMonths(7).AddDays(-1);
    }

    /// <summary>
    /// 上周的第一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime LastWeekFirstDay(DateTime datetime)
    {
        int i = Convert.ToInt16(datetime.DayOfWeek);
        if (i == 0) { i = 7; }
        return datetime.AddDays(Convert.ToDouble((0 - i)) - 6);
    }
    /// <summary>
    /// 上周的最后一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime LastWeekEndDay(DateTime datetime)
    {
        int i = Convert.ToInt16(datetime.DayOfWeek);
        if (i == 0) { i = 7; }
       return datetime.AddDays(Convert.ToDouble((6 - i)) - 6); //
    }
    /// <summary>
    /// 上上周第一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime TheWeekBeforeLastFirstDay(DateTime datetime)
    {
        int i = Convert.ToInt16(datetime.DayOfWeek);
        if (i==0) { i = 7;}
        return datetime.AddDays(Convert.ToDouble((0-i))-13);
    }
    /// <summary>
    /// 上上周最后一天
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static DateTime TheWeekBeforeLastEndDay(DateTime datetime)
    {
        int i = Convert.ToInt16(datetime.DayOfWeek);
        if (i == 0) { i = 7; }
        return datetime.AddDays(Convert.ToDouble((6-i)) - 15);
    }
    /// <summary>
    /// 当前月的总天数
    /// </summary>
    /// <returns></returns>
    public static int GetNowMonthCountDays()
    {
        DateTime dtNow = DateTime.Now;
        int days = DateTime.DaysInMonth(dtNow.Year ,dtNow.Month);
        return days;
    }

  • 相关阅读:
    在SSH框架下按条件分页查询
    关于从数据库中取出来的数据在jsp页面上级联显示出来的问题,和ajax处理乱码
    在js中下拉列表值的选定
    java中的日期转换方法
    用js判断输入文本框的内容类型
    将selenium录制的脚本转换为java脚本到Eclipse中运行(一)
    selenium录制并回放请求-心得(一)
    selenium java版本的安装方法与注意事项
    python编写的面向对象的XXE自动化检测工具(对单个功能进行检测)
    微服务&spring cloud架构系列
  • 原文地址:https://www.cnblogs.com/xinbaba/p/6404530.html
Copyright © 2011-2022 走看看