Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. /**
  2. * 10位时间戳和13位时间戳
  3. * java的时间戳精度是毫秒,所以生成的是13位的
  4. * 而像c++或者php生成的时间戳默认就是10位的,其精度是秒
  5. */
  6.  
  7. // 13位时间戳
  8. const timestamp13 = 1559712343273;
  9. // 方法一
  10. let date = new Date(timestamp13);
  11. console.log(date); // 2019-06-05T05:25:43.273Z
  12. // 方法二
  13. date = new Date();
  14. date.setTime(timestamp13);
  15. console.log(date); // 2019-06-05T05:25:43.273Z
  16. // day of the month
  17. console.log(date.getDate()); // 5
  18. // day of the week
  19. console.log(date.getDay()); // 3
  20. // get the year
  21. console.log(date.getFullYear()); // 2019
  22. // get the minutes of a date
  23. console.log(date.getUTCMinutes()); // 25
  24.  
  25. // 10位时间戳
  26. const timestamp10 = 1560742200;
  27. // 先*1000,转换成毫米
  28. date = new Date(timestamp10 * 1000);
  29. console.log(date);
  30. // 转换成字符串 2019-06-17T03:30:00.000Z
  31. console.log(date.toISOString());
  32.  
  33. // 使用moment也需要将精度转成毫秒
  34. const custom_time = moment(1560742200 * 1000).format("YYYY-MM-DD HH:mm:ss");
  35. console.log(custom_time);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement