function getFromNow( timestamp: number, now?: Date | string | number ): string { const nowDate = now ? now instanceof Date ? now : new Date(now) : new Date() const def = nowDate.getTime() - timestamp // 0時時点比較用 const nowDate0 = new Date( nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0 ) const tgtDate = new Date(timestamp) const tgtDate0 = new Date( tgtDate.getFullYear(), tgtDate.getMonth(), tgtDate.getDate(), 0, 0, 0 ) const defAt0 = nowDate0.getTime() - tgtDate0.getTime() if (defAt0 < 1000 * 60 * 60 * 24) { // 今日の出来事 if (def < 1000 * 60) { // 1分未満 ※指定された日時が未来だったら「たった今」にまるめる return 'たった今' } else if (def < 1000 * 60 * 60) { // 60分未満 return Math.floor(def / (1000 * 60)) + '分前' } else { // 24時間未満 ※ただし今日の範囲内 return Math.floor(def / (1000 * 60 * 60)) + '時間前' } } else { // 0時時点の比較が1日以上経っている if (defAt0 <= 1000 * 60 * 60 * 24) { return '昨日' } else if (defAt0 <= 1000 * 60 * 60 * 24 * 7) { // 7日以下 return Math.floor(defAt0 / (1000 * 60 * 60 * 24)) + '日前' } else { if (nowDate0.getFullYear() === tgtDate0.getFullYear()) { return tgtDate0.getMonth() + 1 * 1 + '/' + tgtDate0.getDate() } else { return ( tgtDate0.getFullYear() + '/' + (tgtDate0.getMonth() + 1 * 1) + '/' + tgtDate0.getDate() ) } } } }