Adding Am/Pm to timestamp - angularjs

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.

Related

Format date in angularJS

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.

Convert date format from object containing dates in AngularJS

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.

Getting AngularUI Bootstrap datepicker to recognize YYYYMMDD format?

We have legacy birth date data in the format of YYYYMMDD (20151022). Angular and the ui-bootstrap datepicker obviously don't like this format. Also, our new UI requirements are to display the format as MMM, d YYYY (Oct, 22 2015). I'm not seeing a way to enforce a non-standard date format (for data, not for display) in the documentation. Is this not supported or am I just overlooking it?
I assume your datepicker is bound to a variable - ng-model="date". Then simply $watch this variable and do the nessecary formatting when a string is assigned to it :
$scope.date = '';
$scope.$watch('date', function(newValue, oldValue) {
if (typeof newValue == 'string') {
var tempDate = new Date(
newValue.substr(4,2)+'-'+
newValue.substr(6,2)+'-'+
newValue.substr(0,4)
);
$scope.date = !isNaN(tempDate.getTime()) ? tempDate : new Date();
}
})
This will return a valid date object if you have assigned a string to date on the format yyyymmdd; if something has gone wrong date will be set to today.
$scope.date = '20151022'; //set the datepicker to 10-22-2015
$scope.date = new Date('01-01-1900') //etc works as usual
In order to use a display format on the form Oct, 22 2015 you are almost right, it should just be lowercase y's :
uib-datepicker-popup="MMM, d yyyy"
the above in a plnkr -> http://plnkr.co/edit/ne60bBaTuca7wajTHP9w?p=preview

Convert year in buddhist format

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!

Why does angular date filter adding 2 to hour?

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'}}

Resources