SHARE
TWEET

Untitled

a guest Jul 24th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 格式化时间戳 格式:yy/mm/dd hh:MM
  3.  * @param time 时间戳
  4.  * @returns {string} yy/mm/dd hh:MM
  5.  */
  6. function formatTime(time) {
  7.     var date = new Date(time);
  8.     var year = date.getFullYear();
  9.     var month = date.getMonth() + 1;
  10.     var days = date.getDate();
  11.     var hours = date.getHours();
  12.     var mins = date.getMinutes();
  13.     return year + '/' + transFormat(month) + '/' + transFormat(days) + ' ' + transFormat(hours) + ':' + transFormat(mins);
  14. }
  15.  
  16. /**
  17.  * 补齐一位数字前面的0,使得输出美观
  18.  * @param value
  19.  * @returns {string}
  20.  */
  21. function transFormat(value) {
  22.     return ('0' + value).slice(-2);
  23. }
  24.  
  25. document.write(formatTime(1509019200000));
  26.  
  27. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  28.  
  29. /**
  30.  * 格式化时间戳(f带字符串的) 格式:yy/mm/dd hh:MM
  31.  * @param time 时间戳
  32.  * @returns {string} yy/mm/dd hh:MM
  33.  */
  34. function formatTime(time,value) {
  35.     var date = new Date(time);
  36.     var year = date.getFullYear();
  37.     var month = date.getMonth() + 1;
  38.     var days = date.getDate();
  39.     var hours = date.getHours();
  40.     var mins = date.getMinutes();
  41.     return value.replace(/(yy)+/i,year).replace(/mm/,transFormat(month)).replace(/dd/i,transFormat(days)).replace(/hh/i,transFormat(hours)).replace(/MM/,transFormat(mins));
  42. }
  43.  
  44. document.write(formatTime(1509019200000,"yyyy/mm/dd hh:MM"));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top