TimeStampConversion.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ.WinAPI
  6. {
  7. /// <summary>
  8. /// 时间戳转换
  9. /// </summary>
  10. public class TimeStampConversion
  11. {
  12. public TimeStampConversion()
  13. {
  14. }
  15. /// <summary>
  16. /// 时间戳开始时间
  17. /// </summary>
  18. static DateTime TimeStampStartTime
  19. {
  20. get
  21. {
  22. return TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
  23. }
  24. }
  25. /// <summary>
  26. /// DateTime转换为JavaScript时间戳 13位数长度
  27. /// </summary>
  28. /// <param name="t"></param>
  29. /// <returns></returns>
  30. public static long DateTimeToJavaScriptTimeStamp(DateTime t)
  31. {
  32. return (long)(t - TimeStampConversion.TimeStampStartTime).TotalMilliseconds; //相差毫秒数
  33. }
  34. /// <summary>
  35. /// JavaScript时间戳转换为DateTime
  36. /// </summary>
  37. /// <param name="t"></param>
  38. /// <returns></returns>
  39. public static System.DateTime JavaScriptTimeStampToDateTime(long timeStamp)
  40. {
  41. return TimeStampConversion.TimeStampStartTime.AddMilliseconds(timeStamp);
  42. }
  43. /// <summary>
  44. /// DateTime转换为Unix时间戳 10位数长度
  45. /// </summary>
  46. /// <param name="timeStamp"></param>
  47. /// <returns></returns>
  48. public static long DateTimeToUnixTimeStamp(DateTime t)
  49. {
  50. return (long)(t - TimeStampConversion.TimeStampStartTime).TotalSeconds; // 相差秒数
  51. }
  52. /// <summary>
  53. /// Unix时间戳转换为DateTime
  54. /// </summary>
  55. /// <param name="timeStamp"></param>
  56. /// <returns></returns>
  57. public static System.DateTime UnixTimeStampToDateTime(long timeStamp)
  58. {
  59. return TimeStampConversion.TimeStampStartTime.AddSeconds(timeStamp);
  60. }
  61. }
  62. }