LoginSignup
3
3

More than 5 years have passed since last update.

日付処理

Posted at

IE8以前は "2013-05-07T17:33:56+09:00" こういうの解釈できない。
Dateオブジェクトは扱いにくい。

a.js
function date_format(ds) {
  date = new Date(ds);
  if (isNaN(date)){
    ds = ds.replace('T', ' '); // 'T' を半角スペースで置換
    ds = ds.replace('.000Z', ''); // '.000Z' を削除 
    ds = ds.replace(/-/g, '/'); // 全ての '-' を '/' で置換 
    ds = ds.replace(/\+.*/g, ' '); // 全ての '-' を '/' で置換
    date = new Date(ds);
  }
  if (!date) return '';
  return '' + date.getFullYear() + '' + ('00' + (date.getMonth() + 1)).slice(-2) + '' + ('00' + date.getDate()).slice(-2) + '>日';
}

date_format("2013-05-07T17:33:56+09:00"); // 2013年5月7日
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3