欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
 
namespace MEAS.Common
{
    public class TimeHelper
    {
        //定义一个用于保存静态变量的实例
        private static TimeHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private TimeHelper() { }
        public static TimeHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null) instance = new TimeHelper();
                    }
                }
                return instance;
            }
        }
 
        /// <summary>
        /// 获取固定日期范围内的所有日期,以数组形式返回
        ///DateTime begin1 = DateTime.Parse("2021-05-02");
        ///DateTime end1 = DateTime.Parse("2021-05-12");
        ///List<string> days = TimeHelper.Instance.GetDays(begin1, end1);
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        public List<string> GetDays(DateTime startTime, DateTime endTime)
        {
            List<string> listDay = new List<string>();
            DateTime dtDay = new DateTime();
            //循环比较,取出日期;
            for (dtDay = startTime; dtDay.CompareTo(endTime) <= 0; dtDay = dtDay.AddDays(1))
            {
                listDay.Add(dtDay.ToString("yyyy-MM-dd"));
            }
            return listDay;
        }
 
        /// <summary>
        /// 将数字形式的日期转换为标准的日期格式 字符串
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public string FamatDate(string date)
        {
            var dt = DateTime.ParseExact(date, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
            return dt.ToString("yyyy-MM-dd");
        }
 
        /// <summary>
        /// 获取时间间隔时长(分钟)
        /// </summary>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public string GetTimeLength(DateTime beginTime, DateTime endTime)
        {
            try
            {
                string TimeLength = string.Empty;
                TimeSpan begin = new TimeSpan(beginTime.Ticks);
                TimeSpan end = new TimeSpan(endTime.Ticks);
                TimeSpan ts = begin.Subtract(end).Duration();
 
                TimeLength = Convert.ToString(ts.Hours * 60 + ts.Minutes);
 
                return TimeLength;
            }
            catch { }
            return string.Empty;
        }
 
        /// <summary>
        /// 计算区间月份
        ///  DateTime begin = DateTime.Parse("2021-08");
        ///  DateTime end = DateTime.Parse("2022-05");
        ///  List<string> months = TimeHelper.Instance.GetMonths(2021, 8, 2022, 5);
        /// </summary>
        /// <param name="startYear">开始年</param>
        /// <param name="startMonth">开始月</param>
        /// <param name="endYear">结束年</param>
        /// <param name="endMonth">结束月</param>
        /// <returns></returns>
        public List<string> GetMonths(int startYear, int startMonth, int endYear, int endMonth)
        {
            List<TimeFormat> list = new List<TimeFormat>();   //计算的区间结果存放在集合
            List<string> ymList = new List<string>();
            if (startYear <= endYear)
            {
                int ComputeYear = endYear - startYear;  //计算开始年和结束年差值
                if (ComputeYear > 0)//跨年情况
                {
                    //循环开始年到结束年
                    for (int i = startYear; i <= startYear + ComputeYear; i++)
                    {
                        if (i == endYear)
                        {
                            for (int lastYear = 1; lastYear <= endMonth; lastYear++)
                            {
                                list.Add(new TimeFormat() { Year = i, Month = lastYear });
                            }
                        }
                        else
                        {
                            if (i == startYear)
                            {
                                for (int lastMonth = startMonth; lastMonth <= 12; lastMonth++)
                                {
                                    list.Add(new TimeFormat() { Year = i, Month = lastMonth });
                                }
                            }
                            else
                            {
                                for (int quyue = 1; quyue <= 12; quyue++)
                                {
                                    list.Add(new TimeFormat() { Year = i, Month = quyue });
                                }
                            }
                        }
                    }
                }
                else //同年情况
                {
                    for (int i = startMonth; i <= endMonth; i++)
                    {
                        list.Add(new TimeFormat() { Year = startYear, Month = i });
                    }
                }
 
                foreach (var item in list)
                {
                    //发现月份会少0,判断小于10月补0
                    var months = "";
                    if (item.Month < 10) months = "0" + item.Month;
                    else months = Convert.ToString(item.Month);
 
                    ymList.Add(item.Year + "-" + months);
                }
            }
            return ymList;
        }
 
        /// <summary>
        /// 获取指定年份区间中的年份
        ///  List<string> years = TimeHelper.Instance.GetYears(2018, 2022);
        /// </summary>
        /// <param name="startYear">开始年份</param>
        /// <param name="endYear">结束年份</param>
        /// <returns></returns>
        public List<string> GetYears(int startYear, int endYear)
        {
            List<string> year = new List<string>();
            for (int i = startYear; i <= endYear; i++)
            {
                year.Add(i.ToString());
            }
            return year;
        }
 
    }
 
    /// <summary>
    /// 计算年月区间
    /// </summary>
    public class TimeFormat
    {
        /// <summary>
        /// 年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 月
        /// </summary>
        public int Month { get; set; }
    }
}

  

posted on   sunwugang  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2016-10-28 Winfrom Wait 实现转圈等待 this.Cursor = Cursors.WaitCursor;
点击右上角即可分享
微信分享提示