egirlskey/src/web/app/common/tags/time.tag

42 lines
1.5 KiB
Plaintext
Raw Normal View History

2017-01-11 20:55:38 +00:00
<mk-time>
<time datetime={ opts.time }><span if={ mode == 'relative' }>{ relative }</span><span if={ mode == 'absolute' }>{ absolute }</span><span if={ mode == 'detail' }>{ absolute } ({ relative })</span></time>
2017-01-11 20:55:38 +00:00
<script>
2017-02-20 00:53:57 +00:00
this.time = new Date this.opts.time
this.mode = this.opts.mode || 'relative'
this.tickid = null
2016-12-28 22:49:51 +00:00
2017-02-20 00:53:57 +00:00
this.absolute =
2017-02-20 02:02:43 +00:00
this.time.getFullYear() + '年' +
this.time.getMonth() + 1 + '月' +
this.time.getDate() + '日' +
2017-01-11 20:55:38 +00:00
' ' +
2017-02-20 02:02:43 +00:00
this.time.getHours() + '時' +
this.time.getMinutes() + '分'
2016-12-28 22:49:51 +00:00
2017-02-20 00:53:57 +00:00
this.on('mount', () => {
2017-02-20 02:02:43 +00:00
if this.mode == 'relative' or this.mode == 'detail'
this.tick!
this.tickid = setInterval this.tick, 1000ms
2016-12-28 22:49:51 +00:00
2017-02-20 00:53:57 +00:00
this.on('unmount', () => {
2017-02-20 02:02:43 +00:00
if this.mode == 'relative' or this.mode == 'detail'
clearInterval this.tickid
2016-12-28 22:49:51 +00:00
2017-02-20 01:34:57 +00:00
this.tick = () => {
2017-02-20 02:02:43 +00:00
const now = new Date();
ago = (now - this.time) / 1000ms
2017-02-20 00:53:57 +00:00
this.relative = switch
2017-01-11 20:55:38 +00:00
| ago >= 31536000s => ~~(ago / 31536000s) + '年前'
| ago >= 2592000s => ~~(ago / 2592000s) + 'ヶ月前'
| ago >= 604800s => ~~(ago / 604800s) + '週間前'
| ago >= 86400s => ~~(ago / 86400s) + '日前'
| ago >= 3600s => ~~(ago / 3600s) + '時間前'
| ago >= 60s => ~~(ago / 60s) + '分前'
| ago >= 10s => ~~(ago % 60s) + '秒前'
| ago >= 0s => 'たった今'
| ago < 0s => '未来'
| _ => 'なぞのじかん'
2017-02-20 00:53:57 +00:00
this.update();
2017-01-11 20:55:38 +00:00
</script>
</mk-time>