我正在努力处理 angularJS 中的一些日期时间格式。
我想获取以下日期格式25/11/1990 14:35
基于包含 “1990-11-25 14:35:00”
的日期时间字符串
,而无需在 Controller 中创建 Date
对象。
如果输入是 Date
对象或者 String
不包含小时或分钟,则 Angular 似乎只能实现正确的日期时间格式
index.html
<div ng-app ng-controller="Ctrl">
String: {{'1990-11-25 14:35:00' | date:"dd/MM/yyyy HH:mm"}}<br>
string date: {{'1990-11-25' | date:"dd/MM/yyyy"}}<br>
Date: {{myDate | date:"dd/MM/yyyy HH:mm"}}
</div>
Controller .js
function Ctrl($scope)
{
$scope.myDate = new Date("1990-11-25 14:35:00");
}
输出
string: 1990-11-25 14:35:00
string date: 25/11/1990
date: 25/11/1990 14:35
http://jsfiddle.net/CkBWL/612/
根据angular's documentation on the date filter , 日期时间字符串允许的唯一格式是:
datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ
有没有办法直接将包含 "1990-11-25 14:35:00"
的字符串格式化为 25/11/1990 14:35
html 而无需创建 Date
对象?
感谢您的帮助!
最佳答案
It seems like angular can only achieve proper datetime formatting if the input is a Date object or if the String does not contain hours or minutes
如果您希望字符串被识别为日期,您应该使用根据 ISO8601 格式化的日期。标准。因此 1990-11-25 14:35:00
变为 1990-11-25T14:35:00Z
,如果需要,您还可以包含偏移量(它在规范中) )。然后您可以使用现有的 date filter内置于 Angular 中。
来自date filter documentation关于参数日期
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
代码更新
<div ng-app ng-controller="Ctrl">
String: {{'1990-11-25T14:35:00Z' | date:"dd/MM/yyyy HH:mm"}}
<br>
Date: {{date | date:"dd/MM/yyyy HH:mm"}}
</div>
function Ctrl($scope) {
$scope.date = "1990-11-25T14:35:00Z";
}
关于javascript - Angular 格式 yyyy-MM-dd HH :mm datetime string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36845589/