Time formatting function

implement the formatTime (dateStr, format) method, and the parameter dateStr is date.toISOString ()

parameter format supports" YYYY","MM","M","DD","D","hh","h","mm","m","ss","s"

reference example:
enter formatTime ("2016-08-29T02 DD DD day")

output: August 29, 2016

Nov.15,2021

moment can fulfill your needs
moment ('2016-08-29T02 DD 03.863Z'). Format (' YYY').


conventional scheme , if it is too big, consider DAYJS .


/**
 * 
 *
 * @param {String, Number} timestamp ()
 * @param {String} format ()
 *    format='YYYY-MM-DD'
 *    format='MM/DD hh:mm'
 * @returns {String} default return YYYY/MM/DD hh:mm:ss
 */
function formatTimestamp(timestamp, format = 'YYYY/MM/DD hh:mm:ss') {
  let time = Number.parseInt(timestamp, 10);
  let date = new Date(time);

  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  let second = date.getSeconds();

  month = month > 9 ? month : `0${month}`;
  day = day > 9 ? day : `0${day}`;
  hour = hour > 9 ? hour : `0${hour}`;
  minute = minute > 9 ? minute : `0${minute}`;
  second = second > 9 ? second : `0${second}`;

  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('hh', hour)
    .replace('mm', minute)
    .replace('ss', second);
}
Menu