• C#根据时间范围获取每年每月每周的分组


    简介:C#根据时间范围获取每年每月每周的分组,这个时间范围可以是多年(2012.01.01-2015.12.31),

               也可以是一年中的几个月(2015.01.01-2015.12.31),也可以是月个月中的几个星期

               (2015.01.01-2015.01.21),根据起始日期和结束日期,生成一个以起始日期和结束日期为

               键值对的字典(Dictionary<string, string> ),时间仓促,初步写的算法,以后再优化,目前

               唯一的一点小遗憾就是,月中每周键值对的排序问题。详细代码如下:

    -------------------------------------------------------------------------------------------------------------------------

    根据时间范围获取每年每月每周的分组:

    /// <summary>
    /// 根据时间范围获取每年每月每周的分组
    /// </summary>
    /// <param name="strStartDate">起始时间</param>
    /// <param name="strEndDate">结束时间</param>
    /// <returns>返回每周起始结束键值对</returns>
    /// <remarks>创建人员(日期):★彭振★(150106 11:12)</remarks>
    public static Dictionary<string, string> GetGroupWeekByDateRange(string strStartDate, string strEndDate)
    {
    Dictionary<string, string> dict = new Dictionary<string, string>();

    DateTime dtStartDate = DateTime.Parse(strStartDate);
    DateTime dtEndDate = DateTime.Parse(strEndDate);

    //同年
    if (dtStartDate.Year == dtEndDate.Year)
    {
    GetGroupWeekByYear(dict, dtStartDate, dtEndDate);
    }
    //不同年
    else
    {
    int WhileCount = dtEndDate.Year - dtStartDate.Year;

    //某年一共有多少天
    int YearDay = DateTime.IsLeapYear(dtStartDate.Year) ? 366 : 365;
    DateTime dtTempStartDate = dtStartDate;

    DateTime dtTempEndDate = dtTempStartDate.AddDays(YearDay - dtTempStartDate.DayOfYear);

    //根据时间范围获取每月每周的分组
    GetGroupWeekByYear(dict, dtTempStartDate, dtTempEndDate);

    for (int i = 1; i < (WhileCount + 1); i++)
    {
    //某年某月一共有多少天
    YearDay = DateTime.IsLeapYear(dtTempStartDate.Year + 1) ? 366 : 365;
    dtTempStartDate = DateTime.Parse(DateTime.Parse((dtTempStartDate.Year + 1) + "." + dtTempStartDate.Month + "." + "01").ToString("yyyy.MM.dd"));
    dtTempEndDate = dtTempStartDate.AddDays(YearDay - dtTempStartDate.DayOfYear);

    //根据时间范围获取每月每周的分组
    GetGroupWeekByYear(dict, dtTempStartDate, dtTempEndDate);

    }
    }

    return dict;
    }
    -------------------------------------------------------------------------------------------------------------------------

    根据时间范围(年)获取每月每周的分组:


    /// <summary>
    /// 根据时间范围(年)获取每月每周的分组
    /// </summary>
    /// <param name="dict">每周起始结束键值对</param>
    /// <param name="strStartDate">起始时间</param>
    /// <param name="strEndDate">结束时间</param>
    /// <remarks>创建人员(日期):★彭振★(150106 13:58)</remarks>
    public static void GetGroupWeekByYear(Dictionary<string, string> dict, DateTime dtStartDate, DateTime dtEndDate)
    {
    //不同月
    if ((dtEndDate.Month - dtStartDate.Month) >= 1)
    {
    int WhileCount = dtEndDate.Month - dtStartDate.Month;

    //某年某月一共有多少天
    int MonthDay = DateTime.DaysInMonth(dtStartDate.Year, dtStartDate.Month);
    DateTime dtTempStartDate = dtStartDate;
    DateTime dtTempEndDate = dtTempStartDate.AddDays(MonthDay - 1);

    //根据时间范围获取每月每周的分组
    GetGroupWeekByMonth(dict, dtTempStartDate, dtTempEndDate);

    for (int i = 1; i < (WhileCount + 1); i++)
    {
    //某年某月一共有多少天
    MonthDay = DateTime.DaysInMonth(dtTempStartDate.Year, dtTempStartDate.Month + 1);
    dtTempStartDate = DateTime.Parse(DateTime.Parse(dtTempStartDate.Year + "." + (dtTempStartDate.Month + 1) + "." + "01").ToString("yyyy.MM.dd"));
    dtTempEndDate = dtTempStartDate.AddDays(MonthDay - 1);

    //根据时间范围获取每月每周的分组
    GetGroupWeekByMonth(dict, dtTempStartDate, dtTempEndDate);

    }
    }
    //同月
    else
    {
    //根据时间范围获取每月每周的分组
    GetGroupWeekByMonth(dict, dtStartDate, dtEndDate);
    }
    }
    -------------------------------------------------------------------------------------------------------------------------

    根据时间范围(月)获取每月每周的分组


    /// <summary>
    /// 根据时间范围(月)获取每月每周的分组
    /// </summary>
    /// <param name="dict">每周起始结束键值对</param>
    /// <param name="strStartDate">起始时间</param>
    /// <param name="strEndDate">结束时间</param>
    /// <remarks>创建人员(日期):★彭振★(150106 11:13)</remarks>
    public static void GetGroupWeekByMonth(Dictionary<string, string> dict, DateTime dtStartDate, DateTime dtEndDate)
    {
    //一周
    if ((dtEndDate.Day - dtStartDate.Day) < 7)
    {
    DayOfWeek day = dtStartDate.DayOfWeek;
    string dayString = day.ToString();

    DateTime dtTempStartDate = dtStartDate;
    DateTime dtTempEndDate = dtEndDate;
    DateTime dtTempDate = DateTime.Now;
    switch (dayString)
    {
    case "Monday":
    dict.Add(dtTempStartDate.ToString(), dtTempEndDate.ToString());
    break;
    case "Tuesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+5);
    break;
    case "Wednesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+4);
    break;
    case "Thursday":
    dtTempDate = dtTempStartDate.Date.AddDays(+3);
    break;
    case "Friday":
    dtTempDate = dtTempStartDate.Date.AddDays(+2);
    break;
    case "Saturday":
    dtTempDate = dtTempStartDate.Date.AddDays(+1);
    break;
    case "Sunday":
    dtTempDate = dtTempStartDate;
    break;
    }
    if (!dayString.Equals("Monday"))
    {
    dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString());
    dtTempDate = dtTempDate.Date.AddDays(+1);
    if (DateTime.Compare(dtTempDate, dtEndDate) <= 0)
    {
    dict.Add(dtTempDate.ToString(), dtTempEndDate.ToString());
    }
    }
    }
    //多周
    else
    {
    DayOfWeek day = dtStartDate.DayOfWeek;
    string dayString = day.ToString();

    DateTime dtTempStartDate = dtStartDate;
    DateTime dtTempEndDate = dtEndDate;
    DateTime dtTempDate = DateTime.Now;

    #region 起始

    switch (dayString)
    {
    case "Monday":
    dtTempDate = dtTempStartDate.Date.AddDays(+6);
    break;
    case "Tuesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+5);
    break;
    case "Wednesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+4);
    break;
    case "Thursday":
    dtTempDate = dtTempStartDate.Date.AddDays(+3);
    break;
    case "Friday":
    dtTempDate = dtTempStartDate.Date.AddDays(+2);
    break;
    case "Saturday":
    dtTempDate = dtTempStartDate.Date.AddDays(+1);
    break;
    case "Sunday":
    dtTempDate = dtTempStartDate;
    break;
    }
    dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString());

    dtTempStartDate = dtTempDate.Date.AddDays(+1);
    #endregion

    #region 结束

    day = dtEndDate.DayOfWeek;
    dayString = day.ToString();

    switch (dayString)
    {
    case "Monday":
    dtTempDate = dtEndDate;
    break;
    case "Tuesday":
    dtTempDate = dtEndDate.Date.AddDays(-1);
    break;
    case "Wednesday":
    dtTempDate = dtEndDate.Date.AddDays(-2);
    break;
    case "Thursday":
    dtTempDate = dtEndDate.Date.AddDays(-3);
    break;
    case "Friday":
    dtTempDate = dtEndDate.Date.AddDays(-4);
    break;
    case "Saturday":
    dtTempDate = dtEndDate.Date.AddDays(-5);
    break;
    case "Sunday":
    dtTempDate = dtEndDate.Date.AddDays(-6);
    break;
    }

    dict.Add(dtTempDate.ToString(), dtEndDate.ToString());

    dtTempEndDate = dtTempDate.Date.AddDays(-1);

    #endregion

    int WhileCount = ((dtTempEndDate.Day - dtTempStartDate.Day) / 7);
    if (WhileCount == 0)
    {
    dict.Add(dtTempStartDate.ToString(), dtTempEndDate.ToString());
    }
    else
    {
    for (int i = 0; i < (WhileCount + 1); i++)
    {
    dtTempDate = dtTempStartDate.Date.AddDays(+6);
    dict.Add(dtTempStartDate.ToString(), dtTempDate.ToString());
    dtTempStartDate = dtTempDate.Date.AddDays(+1); ;
    }
    }
    }
    }
    ————————————————
    版权声明:本文为CSDN博主「GlenPeng」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/pengzhen8805/article/details/42460797

    备注,自己修改的算法:

    public Dictionary<DateTime, DateTime> GetGroupWeek(DateTime dtStartDate, DateTime dtEndDate)
    {
    Dictionary<DateTime, DateTime> dict = new Dictionary<DateTime, DateTime>();
    DayOfWeek day = dtStartDate.DayOfWeek;
    string dayString = day.ToString();

    DateTime dtTempStartDate = dtStartDate;
    DateTime dtTempEndDate = dtEndDate;
    DateTime dtTempDate = DateTime.Now;

    switch (dayString)
    {
    case "Monday":
    dtTempDate = dtTempStartDate.Date.AddDays(+6);
    break;
    case "Tuesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+5);
    break;
    case "Wednesday":
    dtTempDate = dtTempStartDate.Date.AddDays(+4);
    break;
    case "Thursday":
    dtTempDate = dtTempStartDate.Date.AddDays(+3);
    break;
    case "Friday":
    dtTempDate = dtTempStartDate.Date.AddDays(+2);
    break;
    case "Saturday":
    dtTempDate = dtTempStartDate.Date.AddDays(+1);
    break;
    case "Sunday":
    dtTempDate = dtTempStartDate;
    break;
    }

    if (DateTime.Compare(dtEndDate, dtTempDate) <= 0)
    {
    dict.Add(dtStartDate, dtEndDate);
    return dict;
    }
    dict.Add(dtTempStartDate, dtTempDate);

    dtTempStartDate = dtTempDate.Date.AddDays(+1);


    day = dtEndDate.DayOfWeek;
    dayString = day.ToString();

    switch (dayString)
    {
    case "Monday":
    dtTempDate = dtEndDate;
    break;
    case "Tuesday":
    dtTempDate = dtEndDate.Date.AddDays(-1);
    break;
    case "Wednesday":
    dtTempDate = dtEndDate.Date.AddDays(-2);
    break;
    case "Thursday":
    dtTempDate = dtEndDate.Date.AddDays(-3);
    break;
    case "Friday":
    dtTempDate = dtEndDate.Date.AddDays(-4);
    break;
    case "Saturday":
    dtTempDate = dtEndDate.Date.AddDays(-5);
    break;
    case "Sunday":
    dtTempDate = dtEndDate.Date.AddDays(-6);
    break;
    }

    DateTime lastWeekStartDate = dtTempDate;

    dtTempEndDate = dtTempDate.Date.AddDays(-1);

    if (DateTime.Compare(dtTempEndDate, dtTempStartDate) > 0)
    {
    TimeSpan ts1 = new TimeSpan(dtTempStartDate.Ticks);
    TimeSpan ts2 = new TimeSpan(dtTempEndDate.Ticks);
    TimeSpan ts3 = ts2.Subtract(ts1); //ts2-ts1
    long totalDays = (long)ts3.TotalDays + 1;
    long WhileCount = (totalDays / 7);
    if (WhileCount == 0)
    {
    dict.Add(dtTempStartDate, dtTempEndDate);
    }
    else
    {
    for (int i = 0; i < WhileCount; i++)
    {
    dtTempDate = dtTempStartDate.Date.AddDays(+6);
    dict.Add(dtTempStartDate, dtTempDate);
    dtTempStartDate = dtTempDate.Date.AddDays(+1); ;
    }
    }
    }
    dict.Add(lastWeekStartDate, dtEndDate);
    return dict;
    }

  • 相关阅读:
    框架应用; Bootstrap4 框架
    框架做抖动、框架做日期
    图片轮播
    锤子手机的部分页面
    js练习题:说明事件、经过或移出时出现按钮等、网页换肤、显示隐藏、下拉、横向导航点击(移上)下拉、选项卡、进度条
    js常用对象:点击、双击、onload事件、鼠标相关、onblur事件和onfocus事件等;通过循环给多个元素添加事件、通过addEventListener() 方法 监听事件函数
    js数组、对象、函数基础知识点及数组类习题练习
    判断某个对象是不是数组
    关于使用绝对定位使元素垂直居中的问题
    mousewheel事件的兼容方法
  • 原文地址:https://www.cnblogs.com/liuslayer/p/12495684.html
Copyright © 2020-2023  润新知