|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace HealthMonitor.Common
- {
- public class DateTimeUtil
- {
-
-
-
-
-
-
-
- public static long ConvertToTimeStamp(DateTime time)
- {
- long intResult = 0;
- DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Utc, TimeZoneInfo.Local);
- intResult = (long)(time - startTime).TotalMilliseconds;
- return intResult;
- }
-
-
-
-
-
-
-
-
- public static DateTime GetDateTimeFromUnixTimeMilliseconds(long milliseconds)
- {
- DateTimeOffset dt0 = DateTimeOffset.FromUnixTimeMilliseconds(milliseconds);
-
-
-
-
-
- DateTime dt2 = dt0.LocalDateTime;
- return dt2;
- }
-
-
-
-
-
-
- public static bool IsDateTime(string strDate)
- {
- if (strDate.Length != 19)
- return false;
-
- try
- {
- DateTime dt = DateTime.Parse(strDate);
- return true;
- }
- catch
- {
- return false;
- }
- }
-
-
-
-
-
- public static string ThisMonthFirstDate
- {
- get
- {
- DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
- return dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
-
-
-
-
-
- public static string ThisMonthLastDate
- {
- get
- {
- DateTime dt = new DateTime(DateTime.Today.Year, DateTime.Today.Month + 1, 1);
- dt = dt.AddDays(-1);
- return dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
-
-
-
-
-
-
- public static string MonthFirstDate(string sThisDate)
- {
- int nYear = Year(sThisDate);
- int nMonth = Month(sThisDate);
- int nDay = 1;
-
- return EncodeDate(nYear, nMonth, nDay) + " 00:00:00";
- }
-
-
-
-
-
-
- public static string MonthLastDate(string sThisDate)
- {
- int nYear = Year(sThisDate);
- int nMonth = Month(sThisDate);
-
- nMonth++;
- if (nMonth == 13)
- {
- nYear++;
- nMonth = 1;
- }
-
- DateTime dt = new DateTime(nYear, nMonth, 1);
- dt = dt.AddDays(-1);
- return dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
- public static DateTime ToDateTime(string strDateTime)
- {
- try
- {
- return DateTime.Parse(strDateTime);
- }
- catch
- {
- return DateTime.Now;
- }
- }
-
-
-
-
-
-
-
- public static DateTime ToTime(string strTime)
- {
- try
- {
- return DateTime.Parse(strTime);
- }
- catch
- {
- return DateTime.Parse("00:00:00");
- }
- }
-
-
-
-
-
-
- public static string ToTimeStr(DateTime time)
- {
- return time.ToString("HH:mm:ss");
- }
-
-
-
-
-
-
-
-
- public static string ToTimeStr(object time, string sFormat)
- {
- if (time == null || time == DBNull.Value)
- return "";
- else
- {
- try
- {
- return ((DateTime)time).ToString(sFormat);
- }
- catch
- {
- return "";
- }
- }
- }
-
-
-
-
-
-
- public static string ToTimeStr(object time)
- {
- return ToTimeStr(time, "HH:mm:ss");
- }
-
-
-
-
-
-
- public static string ToTimeStrFromSecond(int Second)
- {
-
- string sTimeStr = "";
-
- int NewSecond = 0;
- int hour = Math.DivRem(Second, 3600, out NewSecond);
-
- Second = NewSecond;
- NewSecond = 0;
- int minute = Math.DivRem(Second, 60, out NewSecond);
-
-
- if (hour < 10)
- sTimeStr = sTimeStr + "0" + hour.ToString() + ":";
- else
- sTimeStr = sTimeStr + hour.ToString() + ":";
-
- if (minute < 10)
- sTimeStr = sTimeStr + "0" + minute.ToString() + ":";
- else
- sTimeStr = sTimeStr + minute.ToString() + ":";
-
- if (NewSecond < 10)
- sTimeStr = sTimeStr + "0" + NewSecond.ToString();
- else
- sTimeStr = sTimeStr + NewSecond.ToString();
-
- return sTimeStr;
- }
-
-
-
-
-
-
- public static int ToSecondsFromTimeStr(string timeStr)
- {
- DateTime dt = ToTime(timeStr);
- return dt.Hour * 3600 + dt.Minute * 60 + dt.Second;
- }
-
-
-
-
-
-
- public static string ToDateStr(DateTime dt)
- {
- return dt.ToString("yyyy-MM-dd");
- }
-
-
-
-
-
-
-
- public static string ToDateStr(object dt, string defaultStr)
- {
- if (dt == null || dt is System.DBNull)
- return defaultStr;
- else
- return ((DateTime)dt).ToString("yyyy-MM-dd");
- }
-
-
-
-
-
-
- public static string ToDateStr(object dt)
- {
- return ToDateStr(dt, "");
- }
-
-
-
-
-
-
- public static string ToDateTimeStr(DateTime dt)
- {
- return dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
-
- public static string ToDateTimeStrWithMilliSeconds(DateTime dt)
- {
- int nMilliSeconds = dt.Millisecond;
- return dt.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
- }
-
-
-
-
-
-
-
- public static string ToDateTimeStr(object dt, string defaultStr)
- {
- return ToDateTimeStr(dt, defaultStr, "yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
- public static string ToDateTimeStr(object dt)
- {
- return ToDateTimeStr(dt, "", "yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
-
- public static string ToDateTimeStr(object dt, string defaultStr, string sFormat)
- {
- if (dt == null || dt is System.DBNull)
- return defaultStr;
- else
- return ((DateTime)dt).ToString(sFormat);
- }
-
-
-
-
-
- public static string Now
- {
- get
- {
- return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
-
-
-
-
- public static string NowWithMilliSeconds
- {
- get
- {
- DateTime dtNow = DateTime.Now;
- int nMilliSeconds = dtNow.Millisecond;
- return dtNow.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
- }
- }
-
-
-
-
- public static string Today
- {
- get
- {
- return DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
-
-
-
-
-
-
- public static int Year(string dtString)
- {
- int nYear;
-
- nYear = Convert.ToInt32(dtString.Substring(0, 4));
-
- return nYear;
- }
-
-
-
-
-
-
- public static int Month(string dtString)
- {
- int nMonth;
-
- nMonth = Convert.ToInt32(dtString.Substring(5, 2));
-
- return nMonth;
- }
-
-
-
-
-
-
- public static int Day(string dtString)
- {
- int nDay;
-
- nDay = Convert.ToInt32(dtString.Substring(8, 2));
-
- return nDay;
- }
-
-
-
-
-
-
- public static int Hour(string dtString)
- {
- int nHour;
-
- if (dtString.Length != 19 && dtString.Length != 23)
- return 0;
-
- nHour = Convert.ToInt32(dtString.Substring(11, 2));
-
- return nHour;
- }
-
-
-
-
-
-
- public static int Minute(string dtString)
- {
- int nMinute;
-
- if (dtString.Length != 19 && dtString.Length != 23)
- return 0;
-
- nMinute = Convert.ToInt32(dtString.Substring(14, 2));
-
- return nMinute;
- }
-
-
-
-
-
-
- public static int Second(string dtString)
- {
- int nSecond;
-
- if (dtString.Length != 19 && dtString.Length != 23)
- return 0;
-
- nSecond = Convert.ToInt32(dtString.Substring(17, 2));
-
- return nSecond;
- }
-
-
-
-
-
-
-
- public static int DayOfWeek(string dtString)
- {
- DateTime dt = DateTime.Parse(dtString);
- return (int)dt.DayOfWeek;
- }
-
-
-
-
-
-
- public static int DayOfYear(string dtString)
- {
- DateTime dt = DateTime.Parse(dtString);
- return dt.DayOfYear;
- }
-
-
-
-
-
-
-
- public static string AddMonths(string dtString, int nOffset)
- {
- DateTime dt = DateTime.Parse(dtString);
- DateTime dtRes = dt.AddMonths(nOffset);
- return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
-
- public static string AddDays(string dtString, int offset)
- {
- DateTime dt = DateTime.Parse(dtString);
- DateTime dtRes = dt.AddDays(offset);
- return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
- }
-
-
-
-
-
-
-
-
- public static string AddSeconds(string dtString, int offset)
- {
- DateTime dt = DateTime.Parse(dtString);
- DateTime dtRes = dt.AddSeconds(offset);
- return dtRes.ToString("yyyy-MM-dd HH:mm:ss");
- }
-
- public static string AddMilliSeconds(string dtString, int offset)
- {
- DateTime dt = DateTime.Parse(dtString);
- DateTime dtRes = dt.AddMilliseconds(offset);
-
- int nMilliSeconds = dtRes.Millisecond;
- return dtRes.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
- }
-
- public static string AddMilliSeconds(string dtString, double offset)
- {
- DateTime dt = DateTime.Parse(dtString);
- DateTime dtRes = dt.AddMilliseconds(offset);
-
- int nMilliSeconds = dtRes.Millisecond;
- return dtRes.ToString("yyyy-MM-dd HH:mm:ss") + "." + nMilliSeconds.ToString("000");
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static int DaysAfter(string dtFromString, string dtToString)
- {
- DateTime dtFrom = DateTime.Parse(dtFromString).Date;
- DateTime dtTo = DateTime.Parse(dtToString).Date;
- TimeSpan timeSpan = dtTo - dtFrom;
- return (int)timeSpan.TotalDays;
- }
-
-
-
-
-
-
-
- public static int SecondsAfter(string dtFromString, string dtToString)
- {
- DateTime dtFrom = DateTime.Parse(dtFromString);
- DateTime dtTo = DateTime.Parse(dtToString);
- TimeSpan timeSpan = dtTo - dtFrom;
- return (int)timeSpan.TotalSeconds;
- }
-
-
-
-
-
-
-
- public static int MilliSecondsAfter(string dtFromString, string dtToString)
- {
- DateTime dtFrom = DateTime.Parse(dtFromString);
- DateTime dtTo = DateTime.Parse(dtToString);
- TimeSpan timeSpan = dtTo - dtFrom;
- return (int)timeSpan.TotalMilliseconds;
- }
-
-
-
-
-
-
-
-
- public static string EncodeTime(int nHour, int nMinute, int nSecond)
- {
- string sResult = nHour.ToString("00") + ":" + nMinute.ToString("00")
- + ":" + nSecond.ToString("00");
- return sResult;
- }
-
-
-
-
-
-
-
-
- public static string EncodeDate(int nYear, int nMonth, int nDay)
- {
- string sResult = nYear.ToString("0000") + "-" + nMonth.ToString("00")
- + "-" + nDay.ToString("00");
- return sResult;
- }
-
-
-
-
-
-
- public static string GetDatePart(string sDatetime)
- {
- if (sDatetime.Length < 10)
- return "";
- return sDatetime.Substring(0, 10);
- }
-
-
-
-
-
-
- public static string GetTimePart(string sDatetime)
- {
- if (sDatetime.Length == 19)
- return sDatetime.Substring(11, 8);
- else
- return "00:00:00";
- }
-
-
-
-
-
-
- public static string BeginTimeOfDay(string sDate)
- {
- if (sDate.Length > 10)
- sDate = sDate.Substring(0, 10);
- return sDate + " 00:00:00";
- }
-
-
-
-
-
-
- public static string EndTimeOfDay(string sDate)
- {
- if (sDate.Length > 10)
- sDate = sDate.Substring(0, 10);
- return sDate + " 23:59:59.999";
- }
-
-
-
-
-
- public static string ToDateTime14Str(string sDT)
- {
- if (sDT == "")
- return "";
-
- if (sDT.Length != 19)
- throw new Exception("DateTimeUtil.ToDateTime14Str() error : 参数不是19位");
-
- string sRet = sDT.Replace(":", "").Replace("-", "").Replace(" ", "");
- if (sRet.Length != 14)
- throw new Exception("DateTimeUtil.ToDateTime14Str() error : 返回值不是14位");
-
- return sRet;
- }
-
-
-
-
-
-
- public static string FromDateTime14Str(string sDT14)
- {
- if (sDT14 == "")
- return "";
- if (sDT14.Length != 14)
- throw new Exception("DateTimeUtil.FromDateTime14Str() error : 参数不是14位");
-
- string sRet = sDT14.Substring(0, 4) + "-" + sDT14.Substring(4, 2)
- + "-" + sDT14.Substring(6, 2)
- + " " + sDT14.Substring(8, 2) + ":" + sDT14.Substring(10, 2)
- + ":" + sDT14.Substring(12, 2);
- return sRet;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|