Issue
I am struggling with some datetime formating in angularJS.
I would like to obtain the following date format 25/11/1990 14:35
based on a datetime string
containing "1990-11-25 14:35:00"
without having to create a Date
object in the controller.
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
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");
}
Output
string: 1990-11-25 14:35:00
string date: 25/11/1990
date: 25/11/1990 14:35
http://jsfiddle.net/CkBWL/612/
According to angular's documentation on the date filter, the only format allowed for datetime string are:
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
Is there so a way to format a string containing "1990-11-25 14:35:00"
into 25/11/1990 14:35
directly in the html without having to create a Date
object ?
Thanks for your help !
Solution
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
If you want your string recognized as a date you should use the date formatted according to the ISO8601 standard. So 1990-11-25 14:35:00
becomes 1990-11-25T14:35:00Z
, you can also include the offset if you want (its in the spec). You can then use the existing date filter built into Angular.
From the date filter documentation on param date
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.
Code update
<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";
}
Answered By - Igor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.