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; } } }
标签:int,List,DateTime,转化,日期,new,TimeHelper,public From: https://www.cnblogs.com/YYkun/p/16836630.html