Vue

by Alex Kyriakidis

HTML

<script src="https://unpkg.com/dayjs"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.10/locale/zh-cn.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.10/plugin/localizedFormat.js"></script>
<div id="app">
  <h2>Day.js multiple locales and timezones</h2>
  timestamp: <input v-model.number="timestamp">
  <hr>
  <div>
    <button v-for="loc in locales" @click="setLocale(loc)" v-html="loc"></button>
  </div>
  <div>
    <h2>Formats:</h2>
    <ul>
      <li v-for="format in formats">
        <b>{{format}}:</b> {{timestamp | formatDate(format, locale)}}
      </li>
    </ul>
  </div>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}

Vue

new Vue({
  el: "#app",
  data: {
  	timestamp: 1318781876,
    locale: 'en',
    formats: [
      'LT', 'LTS', 'L', "LL", 'LLL', 'LLLL'
    ],
    locales: [
    	'en', 'zh-cn'
    ]
  },
  methods: {
  	setLocale (locale) {
    	this.locale = locale
    	console.log('🚩', locale)
    }
  },
  filters: {
  	formatDate (value, format, locale) {
    	console.log(value, format, locale)
      return dayjs.unix(value).locale(locale).format(format)
    }
  },
  beforeCreate () {
    dayjs.extend(dayjs_plugin_localizedFormat)
  }
})