問題描述
我的日期從數據庫中出來是這樣的:2013-11-21 17:43:20
我正在嘗試使用 Angular 的日期過濾器將它們變成更漂亮的東西,但是...
{{Objected.created |日期:'shortDate'}}
或
{{Objected.created |日期:'YYYY'}}
...只是吐出原始日期時間字符串:2013-11-21 17:43:20
.沒有錯誤.我做錯了什么?
更新我看到 MySQL 的默認日期時間與 Angular 的數據過濾器所期望的不兼容.我正在嘗試像這樣即時轉換它,但它拋出錯誤:
{{ new Date(result.Job.created).toISOString() |日期:'shortDate'}}
我懷疑我無法以我嘗試的方式實例化 Date 類.錯誤是 $parse:syntax 錯誤.
更新
感謝@m59 的幫助,我通過一些小的調整讓它工作......
HTML:
...{{Object.created |dateToISO |日期:'shortDate'}}
JS:
var myApp = angular.module('myApp',[]);myApp.filter('dateToISO', function() {返回函數(輸入){input = new Date(input).toISOString();返回輸入;};});
這個自定義過濾器將默認的 MySQL 日期時間轉換為日期過濾器期望的格式,所以我發送它一個又一個然后瞧".
您需要將日期字符串轉換為 Angular 支持的格式,例如 ISO 8601 格式.你可以這樣轉換:
$scope.Object.created = new Date($scope.Object.created).toISOString();
此處進行現場演示(點擊).
要即時執行此操作,您需要一個自定義過濾器.現場演示(點擊).
標記:
{{Object.created |dateToISO |日期:'shortDate'}}
JavaScript:
app.filter('dateToISO', function() {返回函數(輸入){返回新日期(輸入).toISOString();};});
更新:
這是一種手動轉換日期的簡單方法 (firefox):
app.filter('badDateToISO', function() {返回函數(壞時間){var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");回歸美好時光;};});
My dates come out of the database looking like this: 2013-11-21 17:43:20
I'm trying to user Angular's date filter to turn them into something prettier, but...
{{Objected.created | date:'shortDate'}}
or
{{Objected.created | date:'YYYY'}}
...just spits out the original datetime string: 2013-11-21 17:43:20
. There are no errors. What am I doing wrong?
Update I see that MySQL's default datetime is incompatible with what Angular's data filter expects. I'm attempting to convert it on the fly like this but it's throwing errors:
<li ng-repeat="result in data">{{ new Date(result.Job.created).toISOString() | date:'shortDate'}}</li>
I suspect I can't instantiate the Date class in the way I'm trying. The error is a $parse:syntax error.
Update
Thanks to @m59's help, I got it working with a few minor adjustments...
HTML:
<html ng-app="myApp">
...
{{Object.created | dateToISO | date:'shortDate'}}
JS:
var myApp = angular.module('myApp',[]);
myApp.filter('dateToISO', function() {
return function(input) {
input = new Date(input).toISOString();
return input;
};
});
This custom filter converts the default MySQL datetime into the format that the date filter expects, so I send it throw one then another and "voila".
You need to convert your date string to something supported by Angular, like ISO 8601 format. You could convert it like this:
$scope.Object.created = new Date($scope.Object.created).toISOString();
Live demo here (click).
To do this on the fly, you need a custom filter. Live demo here (click).
Markup:
<div>{{Object.created | dateToISO | date:'shortDate'}}</div>
JavaScript:
app.filter('dateToISO', function() {
return function(input) {
return new Date(input).toISOString();
};
});
Update:
Here's a simple way to convert your date manually (firefox):
app.filter('badDateToISO', function() {
return function(badTime) {
var goodTime = badTime.replace(/(.+) (.+)/, "$1T$2Z");
return goodTime;
};
});
這篇關于無法格式化默認的 MySQL 日期時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!