javascript可以将php这样的日期格式从2018-09-22 05:20:48
更改为Saturday,22 September
吗? iam使用vue.js和laravel。因为这个日期是使用查询从数据库中获取的,所以我不知道如何获取int时间,它将传递给vue组件。
Vue组件:
<tr v-for="(category,index) in allCategories">
<td class="v-align-middle"> <img :src="category.images[0].link" class="img-fluid categoriesImg" alt=""> </td>
<td class="v-align-middle">{{category.category}}</td>
<td class="v-align-middle">{{formatDate(category.created_at)}}</td>
</tr>
<script>
import {mapGetters} from 'vuex';
export default {
name:"category",
computed: {
...mapGetters({
allCategories:'allCategories'
}),
},
created(){
// this.$swal('Hello Vue world!!!');
this.$store.dispatch('GetAllCategories');
},
methods:{
formatDate(date){
let tanggal = new Date(date);
return tanggal;
},
}
}
</script>
结果
"2018-09-21T22:20:48.000Z"
答案 0 :(得分:0)
momentjs是最好的解决方案。
确保设置正确的格式(https://momentjs.com/docs/#/parsing/string-format/)
formatDate(date){
let tanggal = moment(date, 'YYYY-MM-DD HH:mm:ss').format('dddd,DD MMMM');
return tanggal;
},
答案 1 :(得分:0)
您可以使用以下代码:
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var dayNames=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
d = new Date(date);
time = dayNames[d.getDay()] + ", " + d.getDate() + monthNames[d.getMonth()] ;
有关更多信息,您可以在javaScript中检出Date