LoginSignup
0
1

More than 3 years have passed since last update.

moment.jsをVue.jsで使う

Posted at

タイムスタンプを日付に変換したい

YYYY/MM/DD hh:mmの形式で欲しい
スクリーンショット 2020-02-17 12.47.30.png

moment.jsをインストール

npm install moment

フィルターをつくる

filter.js
import moment from "moment";

export const filDate = value => {
  if (value === "") {
    return "";
  }
  return moment.unix(value).format("YYYY/MM/DD HH:mm");
};

export default {
  install(vue) {
    vue.filter("filDate", filDate);

    vue.prototype.$customFilter = {
      filDate
    };
  }
};

main.jsに下記2文を追加して使えるようにする

import filter from "@/plugins/filter"; //インポートする
Vue.use(filter); //使う

使ってみる

<template>
  <div class="today">
    <template v-for="temperature in temperature.todayTemperature">
      {{ temperature.time | filDate }}
    </template>
  </div>
</template>

結果

YYYY/MM/DD hh:mmの形式でとれた😊
スクリーンショット 2020-02-17 15.45.51.png

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