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 !
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";
}
Updated JsFiddle
Related
I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".
this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');
It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe
Also tried converting the string to date but again invalid date message is getting display.
Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.
I guess you need to convert your date having a supported formatted string :
For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
This is valid for Date.parse() or new Date().
So you'll need to parse your string with another method.
This may helps you :
How to convert dd/mm/yyyy string into JavaScript Date object?
In javascript I have some datetime like this
Date: '2017-07-04'
I want to convert it to DateTime like ajax get result.
Expect result like this:
'/Date(1565089870830)/'
How can I make it possible?
You can use Date.parse(). This method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).
var date = Date.parse('2017-07-04');
console.log(date);
The format you're trying to create is a string containing an Epoch timestamp. To create that in JS you can create a Date object from the input string and retrieve the getTime() property. Then it's just a matter of concatenating that value in to the format needed. Try this:
var date = new Date('2017-07-04');
var epoch = date.getTime();
var output = `/Date(${epoch})/`;
console.log(output);
Presumably you're working with an ASP.Net MVC site, given the date format you're trying to build. One thing to note here is that you don't need to use that format when sending DateTime values back to the server. You can send any string so long as it can be bound to a DateTime instance by the ModelBinder. As such I'd recommend using an ISO8601 format instead.
I have a ng-repeat that display the following expression
{{date}} // 2-1-2017
When I use angular-moment
{{date | amDateFormat:'DD'}}
I got 1, which I'm expecting 2. how to make moment know my format is actually dd-mm-yyyy not mm-dd-yyy in the view? I don't want to parse it at my controllers level as it's complicated.
According to the docs, AngularJs has a built-in filter for date which can be used with the following set:
{{ date_expression | date : format : timezone}}
So that you can use it like:
{{ date | date:'dd-MM-yyyy'}}
However if you use moment's date filter, it has to be a moment date (i.e., complete date with timezone defined) object and you can filter it in the same way that angular does.
{{ date | amDateFormat:'dd-MM-yyyy' }}
You can use angular built-in filter for date (as suggested in other answers), if your input is:
a JavaScript Date object
a number that represents milliseconds (this can be also a string)
a string in ISO 8601 format (e.g. yyyy-MM-ddTHH:mm:ss.sssZ, yyyy-MM-dd etc)
Your date is a string with a format that is not ISO 8601, so you have to change the way you parse 2-1-2017 into a date.
You can parse your string in your controller using moment(String, String) as shown in the following example:
angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope) {
$scope.dates = [
// Wrong way to parse string
moment('2-1-2017').toDate(),
// Parse string specifying format
moment('2-1-2017', 'D-M-YYYY').toDate()
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>
<div ng-app="MyApp" ng-controller="AppCtrl">
<div ng-repeat="date in dates">
<span>angular-moment: {{date | amDateFormat:'DD'}}</span>
<span>angular date filter: {{date | date:"dd"}}</span>
</div>
</div>
If you don't want to modify your controller you can use angular moment amParse filter. As the docs says, this filter:
Parses a custom-formatted date into a moment object that can be used with the am-time-ago directive and the other filters.
So in your case, you can use the code shown in the following example:
angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope) {
$scope.dates = [
'2-1-2017', // February 2 2017
'15-1-2017' // January 15 2017
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>
<div ng-app="MyApp" ng-controller="AppCtrl">
<div ng-repeat="date in dates">
<span>angular-moment: {{date | amParse:'D-M-YYYY' | amDateFormat:'DD'}}</span>
</div>
</div>
I think your problem could be related in how you build your date object
new Date("2-1-2017") ; // Wed Feb 01 2017
If I do this, 1 refers to the day, which is correct.
The date filter parse the date based on the object it receive, you should look for a correct Date format for your input date.
You should use the angular filter, like:
{{date | date:'dd-MM-yyyy'}}
I'm getting date from a rest service as
$scope.dob="1989-10-17 00:00:00"
How can I convert /apply filter to this date to look like normal date (without time).
I tried with splicing the time part but this is inside ng-repeat.It's not working
Please note the format date filter understands:
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.
"1989-10-17 00:00:00" can't be parsed. Instead it should be: "1989-10-17T00:00:00Z"
{{ map.thedate }}
The above outputs 2014-06-29 16:43:48
When I try the below code it still shows the same date as above.
{{ map.thedate | date:'medium' }}
your date is not in ISO format. Use a filter to convert your input to a date and then apply the date filter
app.filter("toDate", function () {
return function (input) {
return new Date(input);
}
});
Then in html markup:
{{map.thedate | toDate | date:'medium'}}
Conver your date, here is an example:
for (var i=0; i<map.length; i++) {
var unixTime = (new Date(map[i].thedate)).getTime();
map[i].thedate= unixTime;
}
AngularJS will not accept that format of date, this is an easy solution to iterate over your dataset and convert it to a format it will accept.
Another alternative is to do the same thing about on the server-side. Just iterate over it and convert it to a unix timestamp.
This is a description from the docs. on what it will accept:
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.