-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
54 lines (49 loc) · 1.7 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Class_Helper
{
class Utils
{
/// <summary>
/// 随机数生成
/// </summary>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <returns>随机数</returns>
public static int RandomNum(int min, int max)
{
return new Random().Next(min, max + 1);
}
/// <summary>
/// 将时间转换为 Unix 时间戳
/// </summary>
public static long GetUnix(DateTime dt)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
return (long)(dt - startTime).TotalSeconds;
}
/// <summary>
/// 将 Unix 时间戳转换为时间
/// </summary>
public static DateTime GetDateTime(long timeStamp)
{
DateTime startDt = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
long lTime = timeStamp * 10000000;
TimeSpan toNow = new TimeSpan(lTime);
return startDt.Add(toNow);
}
/// <summary>
/// 计算两个时间的时间差
/// </summary>
public static string[] DateDiff(DateTime dt1, DateTime dt2)
{
TimeSpan ts1 = new TimeSpan(dt1.Ticks);
TimeSpan ts2 = new TimeSpan(dt2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
return new string[]{ ts.Days.ToString(), ts.Hours.ToString(), ts.Minutes.ToString(), ts.Seconds.ToString() };
}
}
}