|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|