js中国标准时间转化为年月日,时间戳

const data1 = 'Fri Jan 10 2020 18:52:45 GMT+0800 (中国标准时间)'
/**
 * @function 中国标准时间转化为时间戳
 * @param {string} date
 * @returns {number} 时间戳
 * @author 天心天地生 2020-1-14
 * */
function dateToMs(date) {
  let result = new Date(date).getTime();
  return result;
}

const data1_timestamp = dateToMs(data1);

console.log('timestamp', dateToMs(data1)); // > timestamp 1578653565000

/**
 * @function 时间戳转化为年月日
 * @param {string} date
 * @returns {number} 时间戳
 * @author 天心天地生 2020-1-14
 * */
const transformTimestamp = (timestamp) => {
  const date = new Date(timestamp);
  const Y = date.getFullYear() + '-';
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  const D = date.getDate() + ' ';
  const h = date.getHours() + ':';
  const m = date.getMinutes();
  // const s = date.getSeconds(); // 秒
  const dateString = Y + M + D + h + m;
  console.log('dateString', dateString); // > dateString 2020-01-10 18:52
  return dateString;
}
transformTimestamp(data1_timestamp); 

你可能感兴趣的:(JS)