zoukankan      html  css  js  c++  java
  • C#时间分割

          public Dictionary<DateTime, TimeRange> GetDateTimeRanges(DateTime startTime, DateTime endTime, int type)
            {
                Dictionary<DateTime, TimeRange> timeDic = new Dictionary<DateTime, TimeRange>();
                switch (type)
                {
                    case 0://月分割
                 
                        TimeRange timeRange = new TimeRange();
                        timeRange.StartTime = startTime;
                        var nextTime= startTime.AddDays(1 - startTime.Day).AddMonths(1).AddHours(-startTime.Hour).AddMinutes(-startTime.Minute).AddSeconds(-startTime.Second);
                        timeRange.EndTime = nextTime;
                        timeDic.Add(startTime, timeRange);
    
                        while (DateTime.Compare(startTime, endTime) <= 0)
                        {
    
                            TimeRange timeRange0 = new TimeRange();
                            var tmpTime = nextTime;
                            timeRange0.StartTime = nextTime;
                            nextTime= nextTime.AddDays(1 - startTime.Day).AddMonths(1).AddHours(-startTime.Hour).AddMinutes(-startTime.Minute).AddSeconds(-startTime.Second);
                            timeRange0.EndTime = nextTime;
                            timeDic.Add(tmpTime, timeRange0);
                            startTime = nextTime;
                        }             
                        break;
                    case 1://季度分割
                        TimeRange timeRange1 = new TimeRange();
                        timeRange1.StartTime = startTime;
                        var nextTime1 = startTime.AddDays(1 - startTime.Day).AddMonths(3).AddHours(-startTime.Hour).AddMinutes(-startTime.Minute).AddSeconds(-startTime.Second);
                        timeRange1.EndTime = nextTime1;
                        timeDic.Add(startTime, timeRange1);
                        while (DateTime.Compare(startTime, endTime) < 0)
                        {
    
                            TimeRange timeRange0 = new TimeRange();
                            var tmpTime = nextTime1;
                            timeRange0.StartTime = nextTime1;
                            nextTime1 = nextTime1.AddDays(1 - startTime.Day).AddMonths(3).AddHours(-startTime.Hour).AddMinutes(-startTime.Minute).AddSeconds(-startTime.Second);
                            timeRange0.EndTime = nextTime1;
                            timeDic.Add(tmpTime, timeRange0);
                            startTime = nextTime1;
                        }
                        break;
                }
    
    
                return timeDic;
            }
            public class TimeRange
            {
                public DateTime StartTime { get; set; }
                public DateTime EndTime { get; set; }
            }
  • 相关阅读:
    Logstash IIS日志采集
    Logstash_Apache日志采集
    k8s 资源管理
    获取hdfs集群信息(fs.defaultFS)
    PHP CMS的pc标签
    流程
    PHP复习
    权限管理
    注册审核
    简单的文件管理程序练习
  • 原文地址:https://www.cnblogs.com/xiaoqiyaozou/p/14278099.html
Copyright © 2011-2022 走看看