I loop through an array of objects that all have a date property. The conditional I set inside the loop to compare the object's date to today's date should take care of just a few of the objects in the array I want kept out because of an old date, however, this conditional is removing all objects in the array for some reason.
It does not work to use getTime(). getTime removes everything from the array. Like I tried here:
constructor ( public navCtrl: NavController,
public modalCtrl: ModalController,
public loading: LoadingController,
public popoverCtrl: PopoverController,
public getPostSrvc: getPostsService) {
this.listOfEvents = [];
let that = this;
function getPostsSuccess (listOfEventsObject) {
for (var i in listOfEventsObject) {
if(listOfEventsObject[i].date.getTime() < Date.now()){
that.listOfEvents.push(listOfEventsObject[i]);
}//close if
}//close for loop
}//close function
}//close constructor
UPDATE My solution:
export class Home {
listOfEvents: Array<any> = [];
parseDate: number;
today : number;
constructor ( //constructor stuff ){
for (var i in listOfEventsObject) {
that.today = Date.now();
that.parseDate = Date.parse(listOfEventsObject[i].date);
if( that.parseDate > that.today){
that.listOfEvents.push(listOfEventsObject[i]);
}//close if
}//close for
}//close constructor
}//close export
If, as mentioned by RobG in a comment, the value of listOfEventsObject[i].date
is a string, you can parse it first, and compare the resulting Date value to Date.now()
:
if (Date.parse(listOfEventsObject[i].date) < Date.now()) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With