- 最后登录
- 2019-12-25
- 注册时间
- 2012-8-24
- 阅读权限
- 90
- 积分
- 71088
- 纳金币
- 52304
- 精华
- 343
|
- /// <summary>
- /// 获取当前时间戳
- /// </summary>
- /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.</param>
- /// <returns></returns>
- public static long GetTimeStamp(bool bflag = true)
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- long ret;
- if (bflag)
- ret = Convert.ToInt64(ts.TotalSeconds);
- else
- ret = Convert.ToInt64(ts.TotalMilliseconds);
- return ret;
- }
- static DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- public static string NormalizeTimpstamp0(long timpStamp)
- {
- long unixTime = timpStamp * 10000000L;
- TimeSpan toNow = new TimeSpan(unixTime);
- DateTime dt = dtStart.Add(toNow);
- return dt.ToString("yyyy-MM-dd");
- }
- /// <summary>
- /// 时钟式倒计时
- /// </summary>
- /// <param name="second"></param>
- /// <returns></returns>
- public string GetSecondString(int second)
- {
- return string.Format("{0:D2}", second / 3600) + string.Format("{0:D2}", second % 3600 / 60) + ":" + string.Format("{0:D2}", second % 60);
- }
- /// 将Unix时间戳转换为DateTime类型时间
- /// </summary>
- /// <param name="d">double 型数字</param>
- /// <returns>DateTime</returns>
- public static System.DateTime ConvertIntDateTime(double d)
- {
- System.DateTime time = System.DateTime.MinValue;
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0));
- Debug.Log(startTime);
- time = startTime.AddSeconds(d);
- return time;
- }
- /// <summary>
- /// 将c# DateTime时间格式转换为Unix时间戳格式
- /// </summary>
- /// <param name="time">时间</param>
- /// <returns>double</returns>
- public static double ConvertDateTimeInt(System.DateTime time)
- {
- double intResult = 0;
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- intResult = (time - startTime).TotalSeconds;
- return intResult;
- }
- /// <summary>
- /// 日期转换成unix时间戳
- /// </summary>
- /// <param name="dateTime"></param>
- /// <returns></returns>
- public static long DateTimeToUnixTimestamp(DateTime dateTime)
- {
- var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
- return Convert.ToInt64((dateTime - start).TotalSeconds);
- }
- /// <summary>
- /// unix时间戳转换成日期
- /// </summary>
- /// <param name="unixTimeStamp">时间戳(秒)</param>
- /// <returns></returns>
- public static DateTime UnixTimestampToDateTime(DateTime target, long timestamp)
- {
- DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
- return start.AddSeconds(timestamp);
- }
复制代码 |
|