父页面引入
<template>
<view class="container">
<date-dormatter :dateStr="1545903266795"></date-dormatter>
</view>
</template>
<script>
import dateDormatter from '@/components/date-formatter.vue';
export default {
components: {
dateDormatter
},
data() {
return {
};
},
onLoad() {
},
methods: {
}
}
</script>
<style lang="scss">
</style>
子页面
<template>
<view>
{{str}}
</view>
</template>
<script>
export default {
name: 'date-formatter',
props:{
dateStr:{
type:String,
default:''
}
},
data() {
return {
str:this.dateStr
};
},
created() {
var inputTime = parseInt(this.str);
//开始转换成年月日
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
// this.str = y+'-'+m+'-'+d+' '+' '+h+':'+minute+':'+second;
this.str = y+'-'+m+'-'+d;
console.log(this.str+"-----");
}
}
</script>
<style>
</style>