2018-06-01T21:49:54Z

【JavaScript】日時と時間をフォーマットする

簡易のフォーマット処理関数です。

日時フォーマット

/**
 * 日時のフォーマット
 * @param {string} format - フォーマット文字列
 * @param {(Date|number|string)} [opt_date=new Date()] - 日時
 * @param {string} [opt_prefix=''] - 前置語(例:'$'の場合、format='$yyyy/$MM/$dd')
 * @param {string} [opt_prefix=''] - 接尾語(例:'$'の場合、format='yyyy$/MM$/dd$')
 * @return {string} 書式文字列
 */
function dateFormat(format, opt_date, opt_prefix, opt_suffix) {
  var pre = (opt_prefix != null)? opt_prefix: '';
  var suf = (opt_suffix != null)? opt_suffix: '';
  var fmt = {};
  fmt[pre+'yyyy'+suf] = function(date) { return ''  + date.getFullYear(); };
  fmt[pre+'MM'+suf]   = function(date) { return('0' +(date.getMonth() + 1)).slice(-2); };
  fmt[pre+'dd'+suf]   = function(date) { return('0' + date.getDate()).slice(-2); };
  fmt[pre+'hh'+suf]   = function(date) { return('0' +(date.getHours() % 12)).slice(-2); };
  fmt[pre+'HH'+suf]   = function(date) { return('0' + date.getHours()).slice(-2); };
  fmt[pre+'mm'+suf]   = function(date) { return('0' + date.getMinutes()).slice(-2); };
  fmt[pre+'ss'+suf]   = function(date) { return('0' + date.getSeconds()).slice(-2); };
  fmt[pre+'SSS'+suf]  = function(date) { return('00'+ date.getMilliseconds()).slice(-3); };
  fmt[pre+'yy'+suf]   = function(date) { return(''  + date.getFullYear()).slice(-2); };
  fmt[pre+'M'+suf]    = function(date) { return ''  +(date.getMonth() + 1); };
  fmt[pre+'d'+suf]    = function(date) { return ''  + date.getDate(); };
  fmt[pre+'h'+suf]    = function(date) { return ''  +(date.getHours() % 12); };
  fmt[pre+'H'+suf]    = function(date) { return ''  + date.getHours(); };
  fmt[pre+'m'+suf]    = function(date) { return ''  + date.getMinutes(); };
  fmt[pre+'s'+suf]    = function(date) { return ''  + date.getSeconds(); };
  fmt[pre+'S'+suf]    = function(date) { return ''  + date.getMilliseconds(); };
  
  var date = opt_date;
  if (date == null) {
    date = new Date();
  } else if (typeof date === 'number' && isFinite(date) && Math.floor(date) === date) {
    date = new Date(date);
  } else if (Object.prototype.toString.call(date) === '[object String]') {
    date = new Date(date);
  }
  
  var result = format;
  for (var key in fmt) {
    if (fmt.hasOwnProperty(key)) {
      result = result.replace(key, fmt[key](date));
    }
  }
  return result;
};

動作例

dateFormat('yyyy/MM/dd hh:mm:ss.SSS');
"2018/05/30 02:01:10.094"

dateFormat('${yyyy}年${MM}月${dd}日', new Date(), '${', '}');
"2018年05月30日"

時間フォーマット

/**
 * 時間のフォーマット
 * @param {string} format - フォーマット文字列
 * @param {number} time - 時間(ms単位)
 * @param {string} [opt_prefix=''] - 前置語(例:'$'の場合、format='$~H:$mm:$ss.$S')
 * @param {string} [opt_prefix=''] - 接尾語(例:'$'の場合、format='~H$:mm$:ss$.S$')
 * @return {string} 書式文字列
 */
function timeFormat(format, time, opt_prefix, opt_suffix) {
  var pre = (opt_prefix != null)? opt_prefix: '';
  var suf = (opt_suffix != null)? opt_suffix: '';
  var fmt = {};
  fmt[pre+'~d'+suf] = function(time) { return ''  + Math.floor(time/86400000);  };
  fmt[pre+'~H'+suf] = function(time) { return ''  + Math.floor(time/3600000); };
  fmt[pre+'~h'+suf] = function(time) { return ''  + Math.floor(time/1800000); };
  fmt[pre+'~m'+suf] = function(time) { return ''  + Math.floor(time/60000); };
  fmt[pre+'~s'+suf] = function(time) { return ''  + Math.floor(time/1000);  };
  fmt[pre+'~S'+suf] = function(time) { return ''  +(time);  };
  fmt[pre+'HH'+suf] = function(time) { return('0' +(Math.floor(time/3600000)%24)).slice(-2);  };
  fmt[pre+'hh'+suf] = function(time) { return('0' +(Math.floor(time/1800000)%24)).slice(-2);  };
  fmt[pre+'mm'+suf] = function(time) { return('0' +(Math.floor(time/60000)%60)).slice(-2);  };
  fmt[pre+'ss'+suf] = function(time) { return('0' +(Math.floor(time/1000)%60)).slice(-2); };
  fmt[pre+'SSS'+suf]= function(time) { return('00'+(time%1000)).slice(-3);  };
  fmt[pre+'d'+suf]  = fmt[pre+'~d'+suf];
  fmt[pre+'H'+suf]  = function(time) { return ''  +(Math.floor(time/3600000)%24); };
  fmt[pre+'h'+suf]  = function(time) { return ''  +(Math.floor(time/1800000)%24); };
  fmt[pre+'m'+suf]  = function(time) { return ''  +(Math.floor(time/60000)%60); };
  fmt[pre+'s'+suf]  = function(time) { return ''  +(Math.floor(time/1000)%60);  };
  fmt[pre+'S'+suf]  = function(time) { return ''  +(time%1000); };
  
  var result = format;
  for (var key in fmt) {
    if (fmt.hasOwnProperty(key)) {
      result = result.replace(key, fmt[key](time));
    }
  }
  return result;
};

動作例

timeFormat('~H:mm:ss.S', 12*1000+345);
"0:00:12.345"

var time = 1;
time = time*24 + 2;
time = time*60 + 34;
time = time*60 + 56;
time = time*1000 + 78;
timeFormat('${~H}:${mm}:${ss}.${S}', time, '${', '}');
"26:34:56.78"
 コメントを書く