LoginSignup
1
0

More than 1 year has passed since last update.

Vue.js + moment.jsで、時刻をリアルタイム更新する方法

Last updated at Posted at 2022-03-24

#はじめに
Vue.jsとmoment.jsを使用して、時刻をリアルタイムで更新する時計を作る。
本文ではTypeScriptを使用。

#moment.jsをインストール

npm install moment

#開発コード
インポートする。

import Moment from 'moment';

###Moment().format()を使用
表示形式は自由に選択できる。

time = Moment().format("YYYY-MM-DD HH:mm:ssZ");

###setIntervalを使用
createdの中にsetIntervalを入れる。1秒毎に新しい時刻を取得。

created() {
  setInterval(() => {
    this.time = Moment().format("YYYY-MM-DD HH:mm:ssZ");
  }, 1000)
}

#参考サイト

1
0
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
1
0