I have a date in this format "2017-06-26 10:21:25.88785". My purpose is to show this kind of format 'dd/MM/yyyy hh:mm:ss', and put this date into a ng-table. So, I write in my ng-table
{{::fondo.aggTms | date:'dd/MM/yyyy hh:mm:ss'}}
Where fondo is my variable and aggTms is the attribute of the main variable.
Unfortunately, it doesn't work, and in my table is shown the first (wrong) format.
Anyone could help me?
You can try rounding of last seconds part Then covert it into date & then try the angular filter. the function to be called in place of fondo.aggTms is
$scope.toDate = function(date){
var res = date.split(":");
var last = window.Math.round(res[2]);
var datestring = res[0]+':'+res[1]+':'+last;
var d = new Date(datestring);
return d;
}
{{::toDate(fondo.aggTms) | date:'dd/MM/yyyy HH:mm:ss'}}
This is the working plunker link: https://plnkr.co/edit/7OHqUmxnjfs89mCMCF3M?p=preview
Now this's only valid if you're considering that last part as seconds in decimal.
Related
How to convert above all the date into YYYY-MM-DD format? I have used this way but it's not working.
$scope.dateDatas = [0:"29-09-2016", 1:"30-09-2016",2:"01-10-2016",3:"02-10-2016",4:"03-10-2016"]
angular.forEach ($scope.dateDatas,
function (value) {
var d = $filter('date')(value, 'yyyy-mm-dd');
console.log(d);
});
How about this:
$scope.dateDatas = ["29-09-2016", "30-09-2016", "01-10-2016", "02-10-2016", "03-10-2016"];
$scope.result = [];
angular.forEach ($scope.dateDatas, function (value) {
var splitValue = value.split("-");
var date = new Date(splitValue[2], splitValue[1] - 1, splitValue[0]);
var res = $filter('date')(date, 'yyyy-MM-dd');
$scope.result.push(res);
console.log(res);
});
Here are the issues:
On the first line, the there shouldn't be any pairs in an array, remove the 'key', and keep the value:
$scope.dateDatas = ["29-09-2016", "30-09-2016", "01-10-2016", "02-10-2016", "03-10-2016"];
As a side note, remember also to put semicolon after the array.
$filter('date') will work on dates, not on strings. You first need to convert your initial string to a date object. I got the code to convert this string to Date from here. Also note that you need to decrease the month value by 1 before passing it to Date constructor because month in Date constructor is 0-based for some reason (expects 0 for January, 1 for February etc):
var splitValue = value.split("-");
var date = new Date(splitValue[2], splitValue[1] - 1, splitValue[0]);
The filter parameter should be 'yyyy-MM-dd' instead of 'yyyy-mm-dd' (capital M's), because small m's represent minutes instead of months and will give you this result:
["2016-00-29", "2016-00-30", "2016-00-01", "2016-00-02", "2016-00-03"]
Also check full example on plunker.
I'm newbie AngularJS and have question. I use Date.now() for create current time. But I want display year in Buddhist format. I use code from this topic -> How to make a ticking clock (time) in AngularJS and HTML. How I can convert year to Buddhist format ? Thanks all.
You will need a filter something like this:-
myapp.filter('bdate', function($filter)
{
return function(input)
{
if(input == null){ return ""; }
var bYears=500;
//Please write the formula for getting the buddhist date here
//Below is a rough conversion of adding 365*bYears days to todays date
// Convert 'days' to milliseconds
var millies = 1000 * 60 * 60 * 24 * 365 * bYears;
var d = new Date(input).getTime()+millies;
var _date = $filter('date')(new Date(d), 'MMM dd yyyy');
return _date.toUpperCase();
};
});
Usage of above filter is as below:-
<span>{{dt | bdate}}</span>
Check the code working at Plunkr here.
Hope this helps!
I have a class which contains date among many other attributes:
Class example {
Date lastUpdate;
}
So everytime there is change I update the lastUpdate by
lastUpdate = new Date();
to get the new time stamp. In the database it gets updated correctly as 02-OCT-14 12.20.35.559000000 PM. However when I try to display it on the UI using angular JS by calling example.lastUpdate it only displays 10/02/2014 12:20:35. I need to know whether it was AM or PM how do I add that ?
you can create and use Angular your own angular filter for this, let's call it AMPMDateFormat and you can use this way {{ example.lastUpdate | AMPMDateFormat }} to display de AM or PM value next the current displayed value.
var app = angular.module('your-app', []);
app.filter("AMPMDateFormat", function () {
return function(input) {
//here you can use [Moment.js][1]
return moment(input).format();
//or you can parse the input value by yourself
//something like this
var parts = input.split(' ');
var datePart = parts[0];
var timeParts = parts[1].split(':');
var hours = parseInt(timeParts[0]);
var meridian = (hours < 12) ? "AM" : "PM";
if (hours == 0) hours = 12;
if (hours > 12) hours -= 12;
return datePart + ", " + hours + ":" + timeParts[1] + " " + meridian;
}
});
Hope this helps.
!Important. Please, in the code all depends on the format of the input value. I'm assuming that the input format will always be "dd/MM/yyyy hh:mm:ss" just like you said that is the way that it's currently displayed. If the format is other this code doesn't works! But the idea still working.
Angularjs allows you to format your date in the UI. See this documentation for more information.
Therefore, you don't really need a function to do this. You could format this in your HTML.
Example Controller snippet:
angular
.module("myApp")
.controller('MyController',
[ '$scope', function($scope) {
$scope.myDate = new Date();
}]
);
Example UI snippet:
<p>{{myDate | date:'yyyy-MM-dd HH:mm:ss a'}}</p>
In the above, myDate is a Date in the Controller that will display in the HTML in the format given. The a portion is the AM/PM marker. Thus, the value would, for example, look like this: 2014-10-07 10:10:02 AM.
I have the following in my HTML:
{{ row.createdDate }}
The date for this is coming from JSON and looks like this: "createdDate=13-08-2013 03:02"
This creates a date: "13-08-2013 03:27"
I changed this to:
{{ row.createdDate | date:'MM/dd/yy HH:mm' }}
Still it creates the same date: "13-08-2013 03:27"
It seems that the date filter is not doing anything for me. Is the problem that the date filter requires the date to be in a specific format and if so what format should my date be in. Here is the format that I am currently using with MS Web API:
json.SerializerSettings.Converters.Add(
new IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy hh:mm" });
This may be someting to do with the datatype of row.createdDate. I think you need to use date type variable expression, as:
First, in controller, you can add one more function to get date type variable from your string type variable, as
$scope.GetCreateDate = function () {
return new Date(row.createdDate);
};
and then use that date type variable in your binding expression, as:
{{ row.GetCreateDate | date:'MM/dd/yy HH:mm' }}
I took Sanjeev's answer combined it with Convert a mySQL date to Javascript date and made a more generic version of it.
Put this in your controller:
$scope.toJsDate = function(str){
if(!str)return null;
var t = str.split(/[- :]/);
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
return d;
}
And put this in your HTML:
{{toJsDate('2013-09-19 02:23:51') | date:'MM/dd/yyyy # h:mma'}}
Another option is to use a filter like: AngularJS: How to format ISO8601 date format?
I've created a duration timer in angular js, using angular 'date' filter.
For some reason, the hour part starting with '2' instead of '0'.
I'm using the filter like this
{{runningDuration | date:'HH:mm:ss'}}
http://jsfiddle.net/rpg2kill/vNdpu/
What am I doing wrong?
It's because you want to format a date but pass it a difference between dates as an integer. Angular then assumes you want new Date(runningDuration).
You can use a filter to convert the date to utc(this probably requires more corner case handling than just number and date). Demo
JS
myApp.filter('utc', [function() {
return function(date) {
if(angular.isNumber(date)) {
date = new Date(date);
}
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
}
} ]);
HTML
{{runningDuration | utc | date:'HH:mm:ss'}}