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.
Related
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.
I'm importing data from Laravel through a GET request by angularJS. The result:
"vacations":[
{
"id":1,
"timeschedule_id":1,
"begindate":"2017-04-28 00:00:00",
"enddate":"2017-04-30 00:00:00",
},
{
"id":2,
"timeschedule_id":1,
"begindate":"2017-05-05 00:00:00",
"enddate":"2017-05-06 00:00:00",
}
]
Note that this vacations JSON Array is inside data JSON Array
Now I have made the following filter to check if the date from getDate() is between the begindate and the enddate in my controller:
$scope.vacation = data.data[0].vacations.filter(function (d) {
if(moment(d.begindate).format("DD-MM-YYYY") <= $scope.getDate(4)
&& $scope.getDate(4) <= moment(d.enddate).format("DD-MM-YYYY")) {
return true;
}
});
The getDate() Function is a simple function and nothing more than this:
$scope.getDate = function($days_forward) {
return moment().add($days_forward + $scope.i, 'days').format("DD-MM-YYYY");
};
This filter returns the correct results but also more results than required. For example this line is also coming through but it shouldn't:
28-05-2017 // Result from getDate()
How do I make sure that only dates between 2017-04-28 00:00:00 - 2017-04-30 00:00:00and 2017-05-05 00:00:00 - 2017-05-06 00:00:00 are returned from my filter?
You can simply use isBetween. Your code could be like the following:
$scope.vacation = data.data[0].vacations.filter(function (d) {
if( $scope.getDate(4).isBetween(d.begindate, d.enddate) ) {
return true;
}
});
$scope.getDate = function($days_forward) {
return moment().startOf('day').add($days_forward + $scope.i, 'days');
}
The getDate function now returns a moment object instead of a string.
If you need you can customize inclusivity:
Version 2.13.0 introduces inclusivity. A [ indicates inclusion of a value. A ( indicates exclusion. If the inclusivity parameter is used, both indicators must be passed.
If the inclusivity parameter is not specified, Moment will default to ().
As a general rule, as stated in the comments, use moment query functions like isSame, isAfter, isBefore etc to compare moment objects instead of formatting them to strings and comparing via <=
I was comparing a string to a momentJS Object so the following steps I implemented to resolve this problem:
Changed the filter to:
if($scope.getMomentDate(4).isBetween(moment(d.begindate), moment(d.enddate),
'days', '[]')) { // [] = all inclusive
return true;
}
Made a helper function getMomentDate() instead of getDate()
$scope.getMomentDate = function($days_forward){
return moment().add($days_forward + $scope.i, 'days');
};
The isBetween method can be used to compare two MomentJS Objects. With the optional fourth parameter of this method i'm able to include every MomentJS Object in the given range.
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
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.