c#一些与时间相关的方法

一、返回两个时间的时间差 00:00:00

public static string SecondToTimeFormat(DateTime starTime, DateTime endTime)
   {
    var ts = starTime - endTime;
    long lSecond = Convert.ToInt64(Math.Ceiling(ts.TotalSeconds));
    long ihours = lSecond / 3600;
    long iMinutes = (lSecond - ihours * 3600) / 60;
    long iSecond = lSecond % 60;
    return $"{ihours}:{iMinutes}:{iSecond}";
   }

二、将秒数转化为时分秒(00:00:00)

 public static string ChangeSeconds(long duration){    
   TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));    
   string str = "";    
   if (ts.Hours > 0)
   {        
       str = String.Format("{0:00}", ts.Hours)+":"
           + String.Format("{0:00}", ts.Minutes) + ":" 
           + String.Format("{0:00}", ts.Seconds);    
   }    
   if (ts.Hours == 0 && ts.Minutes > 0)
   {        
       str = "00:"+ String.Format("{0:00}", ts.Minutes)+":" 
                  + String.Format("{0:00}", ts.Seconds);    
   }    
   if (ts.Hours == 0 && ts.Minutes == 0)
   {        
      str = "00:00:" + String.Format("{0:00}",ts.Seconds);
   }    
   return str;
 }

三、获取两个年份之间的所有年份

public static List<string> YearsBetween(DateTime startDate, DateTime endDate)
   {
     List<string> yearList = new List<string>();
     if (endDate > startDate)
     {
       if (endDate.Year == startDate.Year)
          {
            yearList.Add(Convert.ToString(startDate.Year));
          }
       else
          {
            int num = endDate.Year - startDate.Year;
            for (int i = 0; i < num; i++)
            {
            yearList.Add(Convert.ToString(startDate.Year + i));
            }
          }
     }
    return yearList;
 }

四、获取两个日期之间的月份(月/年)

/// <summary>
/// 获取两个日期之间的月份(月/年)
/// 调用此方法时,将所有月份以(年/月)的形式加入list
/// foreach (var tuple in Tools.MonthsBetween(_startTime, _endTime)) xList.Add(tuple.Item2.ToString() + "/" + tuple.Item1.ToString());
/// </summary>
/// <returns></returns>
public static IEnumerable<Tuple<int, int>> MonthsBetween(DateTime startDate, DateTime endDate)
   {
       DateTime iterator;
       DateTime limit;
       if (endDate > startDate)
       {
         iterator = new DateTime(startDate.Year, startDate.Month, 1);
         limit = endDate;
       }
       else
       {
         iterator = new DateTime(endDate.Year, endDate.Month, 1);
         limit = startDate;
       }
       while (iterator <= limit)
       {
         yield return Tuple.Create(iterator.Month, iterator.Year);
         iterator = iterator.AddMonths(1);
       }
   }

五、获取两个时间内所有的日期

 public static List<string> DaysBetween(DateTime startDate, DateTime endDate)
 {

     TimeSpan ts = endDate - startDate;

     List<string> days = new List<string>();
     for (int i = 0; i < ts.Days + 1; i++)
     {
         days.Add(startDate.AddDays(i).ToShortDateString());
     }
     return days;
 }

六、按年,月返回月的天数

public static void MonthDayCount(int year, int month, out int day)
{
    day = 0;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            day = 31;
        }
        else if (month == 4 || month == 6 || month == 9 || month == 11)
        {
            day = 30;
        }
        else if (month == 2)
        {
            day = 29;
        }
    }
    else
    {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            day = 31;
        }
        else if (month == 4 || month == 6 || month == 9 || month == 11)
        {
            day = 30;
        }
        else if (month == 2)
        {
            day = 28;
        }
    }
}

七、获取本月第1日到今天的日期

public static List<string> GetMonthDay()

{
    var list = new List<string>();
    for (var i = 1; i <= DateTime.Now.Day; i++)
    {
        var day = new DateTime(DateTime.Now.Year, DateTime.Now.Month, i).Day;
        list.Add(Convert.ToString(day));
    }
    return list;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值