I have a JSON file with information in it, there's title, date, etc, etc,
Format in the JSON:
date:"01/01/2001"
My current code to arrange by date
$(element).find('.ct-googleMap--search').val('');
arrayMarker = [];
dataMarkers = data;
dataMarkers.sort(function (a, b) {
return new Date(a.date).getTime() - new Date(b.date).getTime();
});
Now I want to add hiding dates that have passed. I've tried some options, but now I'm here.
Have you looked at the filter function?
var now = new Date();
//filter out old values first, so the sort is working with a smaller result
dataMarkers = data.filter(function(d) {
return d > now;
});
dataMarkers.sort(function (a, b) {
//equal values don't require the date object construction
if (a.date === b.date) {
return 0;
}
return new Date(a.date) > new Date(b.date); //based on borisdiakur's comment
});
Related
I made an API call in a service. ts that gives me all the data from 01/01/21 till now (01/06/21) but I want to show in my table only the data of last day, Is there any way to do this?
All the data is already arranged by date from the oldest to the newest.
Thank you!
For example, This is my interface :
export interface SaleByCustomer {
account_id: number;
doctype: number;
owner?: number;
company: string;
issue_date: Date;
total: number;
currency_id?: string;
}
Because of information security I can't show the data, but I have for each issua_date several records and I want to get in my component.ts only the data for the last day and the day before for comparison.
This is what I've tried in my Service.ts : (I got nothing in my console)
getSale() {
let json = {
"login_id": "xxxxxxxx",
"login_hash": "xxxxxxx",
"login_company": "xx",
"query": {"doctype":[x], "refstatus":[0,1], "issue_date":"2021-05-01 to 2021-05-31"}
}
let body = JSON.stringify(json); //text to input in internet
let today = new Date();
let yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
return this.http.post<any>(this.URL, body).pipe(
filter(a => a.issue_date >= yesterday && a.issue_date < today)
);
}
Component.ts :
ngAfterViewInit() {
this.saleCompareService.getSale().subscribe(
data => {
console.log(data);
})
}
After I get All the data from those 2 days, I want to compare them in a bar chart, what's the best way?
A simple solution if the data is arranged in the order of the dates, it is to retrieve the data in the index tab [n-1] n being the size of the array
var data = tabArray[tabArray.length - 1];
You should read how to ask a correct question. And provide code that shows how you get the data in sevice.ts, date structure.
const activities = [
{ date: new Date('2019-06-28') },
{ date: new Date('2020-05-10') },
{ date: new Date('2021-01-22') }
]
const sortedA = activities.sort(function(a,b){
return new Date(b.date) - new Date(a.date);
});
const sortedAct = activities.sort((a, b) => b.date - a.date);
const sortedActs = activities.slice().sort((a, b) => b.date - a.date);
console.log("sortedA: ", sortedA);
console.log("sortedAct: ", sortedAct);
console.log("sortedActs: ", sortedActs);
//Pipe date format {{valueDate | date: 'dd/MM/yyyy'}}
function date_is_in_the_future(dateAdd){
// turn '2018-01-01' from a string to an object
input_date_object = new Date(dateAdd);
// Get todays date as an object
today_date = new Date();
// if the inpute_date_object is bigger than today_date, it will return false
boolean_result = input_date_object > today_date;
// return result
return boolean_result;
}
What do I have to do to this function in order to use it in my JavaScript?
Just call it;
console.log(date_is_in_the_future('2019-01-01'));
console.log(date_is_in_the_future('2017-01-01'));
function date_is_in_the_future(dateAdd){
//turn '2018-01-01' from a string to an object
input_date_object = new Date(dateAdd)
//Get todays date as an object
today_date = new Date()
//if the inpute_date_object is bigger than today_date, it will return false
boolean_result = input_date_object > today_date
//return result
return boolean_result
}
I have an array of objects with a date formatted in MMMM Do YYYY format. I need to convert this into a UNIX timestamp to arrange them and then convert them back into the readable date format.
However, in doing this. It seems that my changes from within the forEach callback are not applied to the $scope.lalala variable.
My code:
function compare(a, b) {
if (a.date < b.date)
return -1;
if (a.date > b.date)
return 1;
return 0;
}
$scope.lalala = arrayofincompleteorders;
$scope.lalala.forEach(function(hiVanilla, index) {
hiVanilla.date = moment(hiVanilla.date, 'MMMM Do YYYY').format('x');
if (index == $scope.lalala.length - 1) {
$scope.lalala.sort(compare); timestamps as expected
console.log($scope.lalala); //logs the date property with unix
callback();
}
});
console.log($scope.lalala); //logs the date property with unix timestamps, why?
function callback() {
$scope.lalala.forEach(function(order, index) {
console.log(order.date); //unix timestamp
$scope.lalala[index].date = moment(order.date, 'x').format('MMMM Do YYYY');
console.log($scope.lalala[index].date); //formatted timestamp
});
};
Edit: I have the same problem even with the angular.forEach loop in the callback:
function callback(){
angular.forEach($scope.lalala, function(value, key) {
console.log(value.date);
value.date = moment(value.date, 'x').format('MMMM Do YYYY');
console.log($scope.lalala[key].date);
});
console.log("fire!");
$scope.apply();
};
I get the dates to change successfully but then it says that $scope.apply() is not a function which borks the rest of my script.
Edit2:
I got rid of the callback and have everything in one angular.forEach but it still doesn't apply?
$scope.lalala = arrayofincompleteorders;
angular.forEach($scope.lalala, function(hiVanilla, key) {
hiVanilla.date = moment(hiVanilla.date, 'MMMM Do YYYY').format('x');
if (key == $scope.lalala.length - 1) {
$scope.lalala.sort(compare); //timestamps as expected
console.log($scope.lalala); //logs the date property with unix
console.log(hiVanilla.date); //unix timestamp
hiVanilla.date = moment(hiVanilla.date, 'x').format('MMMM Do YYYY');
console.log($scope.lalala[key].date); //formatted timestamp
}
});
console.log($scope.lalala); //logs the date property with unix timestamps, why?
It looks like I was using angular.forEach in a way that it was not designed.
The following worked, basically I just assigned it by pushing it into an empty array rather than trying to alter the array from which I was looping inside of:
$scope.lalala=[];
var log = = arrayofincompleteorders;
angular.forEach(log, function(value, key) {
if(value.complete!="TRUE")
{
i++;
value.date = moment(value.date, 'x').format('MMMM Do YYYY');
this.push(value); //put the new value in $scope.lalala as specified below
}
}, $scope.lalala);
I am trying to find a way to sort posts into two arrays: upcoming and current (upcoming posts are in the future and current have already been posted).
All posts have a scheduledPubDate that is a date string in the format YYYY-MM-DDT00:00:00. and todays date has to be a Date object as it will need to stay relevent (I am using moment())
Is it possible to compare these two different things without having to use a .split and compare the month / day /year separately
angular.forEach(data.items, function (key, index) {
if (moment(key.scheduledPubDate) > moment()) {
$scope.upcomingPosts.push(item[index]);
} else if (moment(key.scheduledPubDate) <= moment()) {
$scope.currentPosts.push(item[index]);
};
});
Presumably you want the string treated as UTC, a simple parser for that is:
// Expected format YYYY-MM-DDT00:00:00
function parseUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0], b[1]-1, b[2], b[3], b[4], b[5]));
}
Note that this doesn't allow for invalid dates. If needed, an extra line if code is required. So now you can do:
if (parseUTC(key.scheduledPubDate) > new Date()) // or Date.now()
You really don't need moment.js for this.
JavaScript's built-in Date object will help you here.
var date = Date.parse('2014-01-21T12:45:13');
date < Date.now() // true
For the purpose of an example, let's assume items is an array of posts:
var items = [{
scheduledPubDate: '2014-01-21T12:45:13'
// ...other keys here
}, {
scheduledPubDate: '2017-03-01T15:21:00'
} // ...and so on
];
Then a reduce operation over items can categorize the posts:
var posts = items.reduce(function (memo, item) {
memo[Date.parse(item.scheduledPubDate) <= Date.now() ? 'current' : 'upcoming'].push(item);
return memo;
}, { current: [], upcoming: [] });
Now posts.current will contain an array of all posts from items whose scheduledPubDate is before the current date, and posts.upcoming will contain an array of all scheduled posts.
Edited to use Date.parse, to avoid unreliable behavior pointed out by RobG. This requires that all dates be in the YYYY-MM-DDT00:00:00 format you specified; if that is not the case, another solution will be required.
You have to specify the date format of the string
var format = "YYYY-MM-DDT00:00:00";
angular.forEach(data.items, function (key, index) {
if (moment(key.scheduledPubDate, format) > moment()) {
$scope.upcomingPosts.push(item[index]);
} else if (moment(key.scheduledPubDate, format) <= moment()) {
$scope.currentPosts.push(item[index]);
};
});
Working example (See the console.log): http://jsbin.com/fejaxiguce/1/edit?html,output
First create an array of elements, in any order, and then use the .sort() method.
http://www.w3schools.com/jsref/jsref_sort.asp
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
Just substitute the above logic with your own. a and b above can be objects.
Okay, I think my problem is a common Javascript thing.
A have a simple ajax call with a success callback.
The ajax call returns a list of products. Now I need a list of these products sorted by Id and sorted by Date.
The list of products contains objects like this one:
{
Id: 12345,
LastModified: "2015-01-05T14:53:18.493Z",
Name: "Beer"
}
Here is my sample code:
var prodsAll;
var prodsNew;
$.ajax({
url:"getProds.php",
success:function(res) {
prodsAll= res;
prodsNew = res;
prodAll.sort(function (a, b) {
return a.Id > b.Id ? 1 : -1;
});
prodNew.sort(function (a, b) {
return new Date(b.LastModified).getTime() - new Date(a.LastModified).getTime();
});
}
});
Whatever I do, it looks that all of my lists are changed!?
When I do:
console.log(res);
console.log(prodsAll);
console.log(prodsNew);
I always get the same result :-/
How is it possible to change the lists independent!?
This is the problem
prodsAll= res;
prodsNew = res;
These have the same reference to a single array, you make a fresh copy of it like this
prodsAll = res;
prodsNew = res.slice();