<script>
import { formatDate } from '@/utils/date.js';
export default {
mounted () {
this.test();
},
methods: {
test() {
let str = '2020-12-20 14:02:23';
console.log(formatDate(Date.parse(str)));
}
}
};
</script>
date.js
export function formatDate (timestamp) {
const arrTimestamp = (timestamp + '').split('')
for (let start = 0; start < 13; start++) {
if (!arrTimestamp[start]) {
arrTimestamp[start] = '0'
}
}
timestamp = arrTimestamp.join('') * 1
const minute = 1000 * 60
const hour = minute * 60
const day = hour * 24
const month = day * 30
const now = new Date().getTime()
const diffValue = now - timestamp
if (diffValue < 0) {
return '不久前'
}
const monthC = diffValue / month
const weekC = diffValue / (7 * day)
const dayC =