123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ant.Service.Utility
- {
- public static class DateTimeUtility
- {
- // 时间戳转为C#格式时间
- public static DateTime StampToDateTime(this string timeStamp)
- {
- DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToUniversalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(timeStamp + "0000000");
- TimeSpan toNow = new TimeSpan(lTime);
- return dateTimeStart.Add(toNow);
- }
- // DateTime时间格式转换为Unix时间戳格式
- public static int UnixDateTimeToStamp(this System.DateTime time)
- {
- var now = TimeZone.CurrentTimeZone.ToUniversalTime(time);
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new System.DateTime(1970, 1, 1));
- return (int)(now - startTime).TotalSeconds;
- }
- /// <summary>
- /// DateTime时间格式转换为Unix时间戳格式
- /// </summary>
- /// <param name="time"> DateTime时间格式</param>
- /// <returns>Unix时间戳格式</returns>
- public static int ConvertDateTimeInt(System.DateTime time)
- {
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- return (int)(time - startTime).TotalSeconds;
- }
- public static DateTime StampToDate(string timeStamp)
- {
- DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- long lTime = long.Parse(timeStamp + "0000000");
- TimeSpan toNow = new TimeSpan(lTime);
- return dateTimeStart.Add(toNow);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static int DateTimeToStamp(this System.DateTime time)
- {
- var now = TimeZone.CurrentTimeZone.ToLocalTime(time);
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- return (int)(now - startTime).TotalSeconds;
- }
- public static UInt32 DateTimeToUInt32(this System.DateTime time)
- {
- var now = TimeZone.CurrentTimeZone.ToUniversalTime(time);
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new System.DateTime(1970, 1, 1));
- return (UInt32)(now - startTime).TotalSeconds;
- }
- /// <summary>
- /// 秒转换小时
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static string SecondToHour(double time)
- {
- string str = "";
- int hour = 0;
- int minute = 0;
- int second = 0;
- second = Convert.ToInt32(time);
- if (second > 60)
- {
- minute = second / 60;
- second = second % 60;
- }
- if (minute > 60)
- {
- hour = minute / 60;
- minute = minute % 60;
- }
- return (hour + "小时" + minute + "分钟"
- + second + "秒");
- }
- /// <summary>
- /// 两个时间之间差(可以计算到几天几时几分几秒)
- /// </summary>
- /// <param name="date1"></param>
- /// <param name="date2"></param>
- /// <returns></returns>
- public static string DateTimeDifference(DateTime date1, DateTime date2)
- {
- DateTime d1 = date1;
- DateTime d2 = date2;
- TimeSpan d3 = d2.Subtract(d1);
- StringBuilder sbstr = new StringBuilder();
- if (d3.Days > 0)
- {
- sbstr.AppendFormat("{0}天", d3.Days);
- }
- if (d3.Hours > 0)
- {
- sbstr.AppendFormat("{0}小时", d3.Hours);
- }
- if (d3.Minutes > 0)
- {
- sbstr.AppendFormat("{0}分钟", d3.Minutes);
- }
- if (d3.Seconds > 0)
- {
- sbstr.AppendFormat("{0}秒", d3.Seconds);
- }
- return sbstr.ToString();
- }
- /// <summary>
- /// 秒转换小时
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- public static string MinuteToHour(double time)
- {
- string str = "";
- int hour = 0;
- int minute = 0;
- int second = 0;
- minute = Convert.ToInt32(time);
- if (minute > 60)
- {
- hour = minute / 60;
- minute = minute % 60;
- }
- return (hour + "小时" + minute + "分钟");
- }
- /// <summary>
- /// 把分钟数转化成几小时几分钟(100 -> 01:40)
- /// </summary>
- /// <param name="minutes">待转化的分钟数</param>
- /// <returns>几小时几分钟</returns>
- public static string ConvertToTime(double minutes)
- {
- int M = 0;//待转化的分钟数;
- int h = 0;//小时
- int m = 0;//分钟
- h = M / 60;
- m = M % 60;
- DateTime time = DateTime.Parse("00:00");
- time = time.AddHours(h).AddMinutes(m);
- string R = time.ToString("HH:mm");
- if (R == "00:00")
- {
- R = ""; ;
- }
- return R;
- }
- }
- }
|