I am using bootstrap-datetimepicker and using ISO8601 datetime format as yyyy-mm-ddThh:ii:ssZ as mentioned in their options section
In my controller, I do
transaction.date = $('.form_datetime input').val();
which sends the data to backend as (console.log)
created_on: "Wed, 08 May 2013 23:18:32 -0000"
and saves in database as
2013-05-08 16:18:32-07
In my template, I do
<td>{{ transaction.created_on | date:'medium'}}</td>
and I see the output on my HTML as
Wed, 08 May 2013 23:18:32 -0000
But as per the Angular doc, it should be for format Oct 28, 2010 8:40:23 PM
What is that I am missing?
I don't understand why no one provided the simple answer of using the correct format in the filter?
{{item.date | date:'yyyy-MM-ddTHH:mm:ssZ'}}
That will format as ISO-8601
For now, I have created a filter
angular.module('customFilters', []).
filter('dateInMillis', function() {
return function(dateString) {
return Date.parse(dateString);
};
});
added as dependency in app.js as
var app = angular.module('myApp', [
'$strap.directives', 'ngCookies', 'categoryServices', 'transactionServices',
'customFilters'
]);
and in HTML used it as
<!-- added dateInMillis to pass to date to filter Angular way -->
<td>{{ transaction.created_on | dateInMillis | date: 'medium'}}</td>
and that presents date on HTML as
May 8, 2013 5:14:36 PM
If you know a better idea, please let me know
In Brazil (and most of Europe, Australia, etc.), we use the default MySQL DATETIME on DB's:
"Y-m-d HH:ii:ss"
But use date() to display it like this:
"d/m/Y HH:ii:ss"
So in order to display that date in angular, "correctly", create a filter:
var app = angular.module(...
// Converts MySQL datetime into readable BR format
/*
Converts 2013-10-18 18:47:15 into 1382122035000 so angular can format date
using brazilian standards
*/
app.filter('brDateFilter', function() {
return function(dateSTR) {
var o = dateSTR.replace(/-/g, "/"); // Replaces hyphens with slashes
return Date.parse(o + " -0000"); // No TZ subtraction on this sample
}
});
Then, on your angular app, just call the filter and format the value for display:
{{ item.datetime_value | brDateFilter | date:"dd/MM/yyyy HH:mm" }}
That covers date format in Brazilian, UK, New Zealand, etc. date format for angular, just subtract the timezone correctly.
Beside all this, just you have change like this,
<td>{{ transaction.created_on | date:'medium'}}</td>
try to wirte it as
<td>{{ transaction.created_on | date:'MMM d, y h:mm:ss a'}}</td>
Related
My date from the database table looks like this:
"2017-12-07 14:42:38.0611177 +00:00"
I'm trying to format it in my HTML like this:
<td>{{ note.CreatedAt | date : "MMM d, y" }}</td>
I was expecting the date to look like this:
Dec 7, 2017
But, the result looks like this:
Can someone show me what I'm doing wrong?
You have to convert your date as a timestamp before filter it.
Aleksey remark is a good point, you can get timestamp from controller:
angular.module('app').controller('dateCtrl', ['$scope', function($scope){
var date = "2017-12-07 14:42:38.0611177 +00:00";
$scope.createdAt = new Date(date).valueOf();
}]);
And use it in your view:
<span>{{createdAt | date : "MMM d, y"}}</span>
Top example will output: Dec 7, 2017
Plunker Demo
For more informations, refer to AngularJS documentation concerning [date filter].2
I'm getting documents from Mongodb into angular. One of the variables is a date. The return data for the date appears in this format:
"dateadded": "Sun Apr 23 2017 18:09:52 GMT-0600 (Mountain Daylight Time)",
I thought I could just apply the angular filter in the HTML.
{{ quote.dateadded | date:'yyyy' }}
for example to format the date. No matter what date filter I try to apply, it always displays Sun Apr 23 2017 18:09:52 GMT-0600 (Mountain Daylight Time).
I can try other filters such as: {{quote.dateadded | limitTo : 10 }} and the it works to show the first 10 characters. That implies to me it is seeing the return data as a string, not a js date object. I've even tried a custom filter to know avail. I'm sure I'm missing something simple in the data conversion but thought Angular handled it as JSON. What am I missing?
your date is a string. need to convert it to a date format in order to apply the date filter.
you can create an another custom filter to convert a string to date
.filter('stringDate',function(){
return function(date){
return new Date(date)
}
})
then use the filters like this
{{quote.dateadded | stringDate | date : 'yyyy'}}
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.quote = {"dateadded": "Sun Apr 23 2017 18:09:52 GMT-0600 (Mountain Daylight Time)"}
})
.filter('stringDate',function(){
return function(date){
return new Date(date)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
{{quote.dateadded | stringDate | date : 'yyyy'}}
</div>
i write the following coding to print the current date time
$scope.date = new Date();
and then i print the same using consol.log
console.log($scope.date);
and it is working fine
Tue Jan 24 2017 16:36:06 GMT+0530 (India Standard Time)
but now i want to change the date format and i want to print like
21-12-2016
can anybody help me here?
i used the conversion but i am unable to remember the page or the url of the page right now,
and stuck on this,
before i leave for the home today i thought of solving this issue
In controller you can do
$filter('date')(date, format, timezone)
to change the date format. And in html,
{{ date_expression | date : format : timezone}}
use this.
Like
$scope.formattedDate = $filter('date')($scope.currDate, "dd-MM-yyyy");
to print same on html
{{ currDate | date : "dd-MM-yyyy"}}
https://docs.angularjs.org/api/ng/filter/date
Following formats are supported by angular.
You can do this either in controller or in html page.
$scope.date = new Date();
The first one is :
$scope.date = $filter('date')($scope.date, 'dd-MM-yyyy');
Second one is :
{{date | date:'dd-MM-yyyy'}}
You can use the Angular date filter:
{{date | date: 'dd-MM-yyyy'}}
You can use the in-build js libraries functions i.e getDay(), getHours(), getMinutes(), getMilliseconds(). This functions will return you the corresponding date's individual components values.
e.g
var x = $scope.yourDateModelObj.getHours();
Likewise, you can get the date, month, years values.
return an integer value for hours.
Hope that helps
I am working with angularjs and the angularjs bootstrap ui (http://angular-ui.github.io/bootstrap/).
I want to use the datepicker directive (http://angular-ui.github.io/bootstrap/#/datepicker).
I am getting a string value from a database
2015-07-30 15:10
Angular wants to have a date object as model but I wasnt able to create a date object with that string. the error I get is
Error: [ngModel:datefmt] http://errors.angularjs.org/1.4.3/ngModel/datefmt?p0=2015-07-30
Can anyboy help me create a date object in angular with a string which has this format -> YYYY-MM-DD
more Information:
the string is in my scope
$scope.event.startdate
and I am splitting it in date, hours and minutes.
$scope.startpoint = {
date: $scope.event.startdate.substring(0, 10),
hours: $scope.event.startdate.substring(11, 13),
mins: $scope.event.startdate.substring(14, 16)
};
The database is MongoDB
Thank you
Adrian
You can use use a generic javascript solution. For example, you can use serega386's answer to a similar javascript question:
var st = "26.04.2013";
var pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
var dt = new Date(st.replace(pattern,'$3-$2-$1'));
You can have the below code in your controller
$scope.newDate =new Date($scope.event.startdate);
and below code in your template
{{newDate | date:'yyyy-MM-dd'}}
Ref: AngularJS/javascript converting a date String to date object
If you are getting the date as a single set of numbers like this
1288323623006
you can use the date formatter directly on that.(from angular doc here)
<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>
Expected result
I hope that solves your problem, let me know if I'm missing something.
As a response from the json I am getting the UTC timezone. I need to convert it to local time.
<span class="text-muted">{{trans.txnDate}}</span>
Can anyone help on this?
I just had to solve this problem as well with dates coming from .NET Web API in the format 'yyyy-MM-ddTHH:mm:ss' (e.g. 2016-02-23T00:11:31) without the 'Z' suffix to indicate UTC time.
I created this filter that extends the angular date filter and ensures that the timezone suffix is included for UTC time.
UTC to Local Filter:
(function () {
'use strict';
angular
.module('app')
.filter('utcToLocal', utcToLocal);
function utcToLocal($filter) {
return function (utcDateString, format) {
if (!utcDateString) {
return;
}
// append 'Z' to the date string to indicate UTC time if the timezone isn't already specified
if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) {
utcDateString += 'Z';
}
return $filter('date')(utcDateString, format);
};
}
})();
Example Usage:
{{product.CreatedDate | utcToLocal:"dd.MM.yyyy"}}
EDIT (2nd Jan 2017): Please refer #Jason's answer, it is better than this one since it uses custom filter to fix the date format - that's the more Angular way of doing it.
My original answer and edits:
You could use the date filter to format the date:
<span class="text-muted">{{trans.txnDate | date:'yyyy-MM-dd HH:mm:ss Z' }}</span>
This will output:
2010-10-29 09:10:23 +0530
(assuming trans.txnDate = 1288323623006;)
See this documentation of date in angularjs.org. It has quite a few examples that are very helpful!
EDIT:
In response to your comment, use the following to get the date as 17 oct 2014:
<span class="text-muted">{{trans.txnDate | date:'dd MMM yyyy' | lowercase }}</span>
Check the documentation link that I mentioned above.
EDIT2:
In response to your other comment, use the following code. The problem is that the string that you are getting is not properly formatted so the Date object is not able to recognise it. I have formatted it in the controller and then passed to the view.
function MyCtrl($scope) {
var dateString = "2014:10:17T18:30:00Z";
dateString = dateString.replace(/:/, '-'); // replaces first ":" character
dateString = dateString.replace(/:/, '-'); // replaces second ":" character
$scope.date = new Date(dateString);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
{{date | date:'dd MMM yyyy' | lowercase }}
</div>
The JS code for replacement can be improved by finding a smarter way to replace the first 2 occurrences of : character.
I had the same issue. AngularJs's date filter doesn't figure out the string is UTC format, but JavaScript Date object does. So I created a simple function in the Controller:
$scope.dateOf = function(utcDateStr) {
return new Date(utcDateStr);
}
And then used something like:
{{ dateOf(trans.txnDate) | date: 'yyyy-MM-dd HH:mm:ss Z' }}
It displays the date/time in the local timezone
I had the same issue. Below Answer
{{trans.txnDate | date:'yyyy-MM-dd HH:mm:ss Z':'+0530' }}
//You can also set '+0000' or another UTX timezome