123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Common.Helper
- {
- public class DateHelper
- {
- public static DateTime GetDateTime(long timestamp)
- {
- DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- long lTime = long.Parse(timestamp + "0000000");
- TimeSpan timeSpan = new TimeSpan(lTime);
- DateTime targetDt = dtStart.Add(timeSpan);
- return targetDt;
- }
- public static DateTime GetDateTime(long timestamp, TimeZoneInfo targetTimeZone)
- {
- DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- long lTime = long.Parse(timestamp + "0000000");
- TimeSpan timeSpan = new TimeSpan(lTime);
- DateTime targetDt = dtStart.Add(timeSpan);
- return TimeZoneInfo.ConvertTimeFromUtc(targetDt, targetTimeZone);
- }
- public static DateTime StampToDateTime(string time)
- {
- time = time.Substring(0, 10);
- double timestamp = Convert.ToInt64(time);
- System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
- dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
- return dateTime;
- }
- public static string TimeSubTract(DateTime time1, DateTime time2)
- {
- TimeSpan subTract = time1.Subtract(time2);
- return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
- }
-
-
-
- public static DateTime ToLocalTimeDateBySeconds(long unix)
- {
- var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
- return dto.ToLocalTime().DateTime;
- }
-
-
-
- public static long ToUnixTimestampBySeconds(DateTime dt)
- {
- DateTimeOffset dto = new DateTimeOffset(dt);
- return dto.ToUnixTimeSeconds();
- }
-
-
-
- public static DateTime ToLocalTimeDateByMilliseconds(long unix)
- {
- var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
- return dto.ToLocalTime().DateTime;
- }
-
-
-
- public static long ToUnixTimestampByMilliseconds(DateTime dt)
- {
- DateTimeOffset dto = new DateTimeOffset(dt);
- return dto.ToUnixTimeMilliseconds();
- }
-
-
-
- public enum Period { Day, Week, Month, Year };
-
-
-
-
-
-
- public static void GetPeriod(Period period, out DateTime beginDate, out DateTime endDate)
- {
- int year = DateTime.Today.Year;
- int month = DateTime.Today.Month;
- switch (period)
- {
- case Period.Year:
- beginDate = new DateTime(year, 1, 1);
- endDate = new DateTime(year, 12, 31);
- break;
- case Period.Month:
- beginDate = new DateTime(year, month, 1);
- endDate = beginDate.AddMonths(1).AddDays(-1);
- break;
- case Period.Week:
- int week = (int)DateTime.Today.DayOfWeek;
- if (week == 0) week = 7;
- beginDate = DateTime.Today.AddDays(-(week - 1));
- endDate = beginDate.AddDays(6);
- break;
- default:
- beginDate = DateTime.Today;
- endDate = DateTime.Today;
- break;
- }
- }
- }
- }
|