• 获取 本周、本月、本季度、本年 半年的开始时间或结束时间


    #region 获取 本周、本月、本季度、本年 半年的开始时间或结束时间
    /// <summary>
    /// 获取开始时间
    /// </summary>
    /// <param name="TimeType">Week、Month、Season、Year</param>
    /// <param name="now"></param>
    /// <returns></returns>
    public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
    {
    switch (TimeType)
    {
    case "Week":
    return now.AddDays(-(int)now.DayOfWeek + 1);
    case "Month":
    return now.AddDays(-now.Day + 1);
    case "Season":
    var time = now.AddMonths(0 - ((now.Month - 1) % 3));
    return time.AddDays(-time.Day + 1);
    case "Year":
    return now.AddDays(-now.DayOfYear + 1);
    case "HalfYear":
    return now.AddMonths(-6);
    default:
    return null;
    }
    }

    /// <summary>
    /// 获取结束时间
    /// </summary>
    /// <param name="TimeType">Week、Month、Season、Year</param>
    /// <param name="now"></param>
    /// <returns></returns>
    public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
    {
    switch (TimeType)
    {
    case "Week":
    return now.AddDays(7 - (int)now.DayOfWeek);
    case "Month":
    return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
    case "Season":
    var time = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
    return time.AddMonths(1).AddDays(-time.AddMonths(1).Day + 1).AddDays(-1);
    case "Year":
    var time2 = now.AddYears(1);
    return time2.AddDays(-time2.DayOfYear);
    case "HalfYear":
    return now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
    default:
    return null;
    }
    }
    #endregion

  • 相关阅读:
    单例模式
    grails2.3.11第二课
    grails2.3.11第一课
    【安全】requests和BeautifulSoup小试牛刀
    【解决】国内访问github过慢
    基于Ubuntu14.10的Hadoop+HBase环境搭建
    基于adt-bundle的Android开发环境搭建
    【解决】SAE部署Django1.6+MySQL
    【解决】Django项目废弃SQLite3拥抱MySQL
    【OpenGL】画立方体
  • 原文地址:https://www.cnblogs.com/canfengfeixue/p/15822288.html
Copyright © 2020-2023  润新知