فهرست منبع

Add formatDate filter

sbkwgh 8 سال پیش
والد
کامیت
0b0a3f31b9
2فایلهای تغییر یافته به همراه27 افزوده شده و 1 حذف شده
  1. 1 1
      src/components/routes/Index.vue
  2. 26 0
      src/main.js

+ 1 - 1
src/components/routes/Index.vue

@@ -26,7 +26,7 @@
 					<td>{{thread.title}}</td>
 					<td>
 						<div>{{thread.latestPostUser}}</div>
-						<div>{{thread.latestPostDate}}</div>
+						<div>{{thread.latestPostDate | formatDate('time|date', ' - ') }}</div>
 					</td>
 					<td>{{thread.category}}</td>
 					<td>{{thread.replies}}</td>

+ 26 - 0
src/main.js

@@ -16,6 +16,32 @@ const router = new VueRouter({
 	mode: 'history'
 })
 
+Vue.filter('formatDate', function (value, format = '', join = ' ') {
+	//Add leading zero if under 10
+	function lz(num) {
+		if(num < 10) {
+			return '0' + num;
+		} else {
+			return '' + num;
+		}
+	}
+
+	function formatSegment(segment) {
+		if(segment === 'time') {
+			return value.toTimeString().slice(0, 5);
+		}
+		if(segment === 'date') {
+			return (
+				lz(value.getDate()) + '/' +
+				lz(value.getMonth() + 1) + '/' +
+				value.getUTCFullYear()
+			);
+		}
+	}
+
+	return format.split('|').map(formatSegment).join(join);
+});
+
 new Vue({
 	el: '#app',
 	template: '<App/>',