I'm rendering local JSON to the browser and each item contains a modified date in ISO 8601 time (such as "2019-03-19T18:50:39Z"). The plan is to make it so that if the Created date is older than 30 days then it will be hidden from the div.
Should I convert the time into a more readable format (such as 03/19/2019)? Would it be easier to work with?
New JSON data could be added at any point, which is why I need to have my code be dynamic. It's part of the reason why I'm struggling with this one.
JS snippet:
import testjson from './test.json';
export default class {
constructor() {
}
loadNewCourses() {
let newCrs = testjson.d.results
.sort(function(a, b) { // sorts by newest
return (a.Created < b.Created) ? 1 : ((b.Created < a.Created) ? -1 : 0)
})
.filter((el, idx, self) => { // no duplicates
return (idx === self.map(el => el.Category).indexOf(el.Category))
})
.map(x => {
return {
"Category": x.Category,
"Title": x.Title,
"Description": x.Description,
"Created": x.Created
}
})
$.each(newCrs, function(idx, val) { // ---- does this look right?
let current = new Date();
let expiry = new Date(val.Created)
if (current.getTime() > expiry.getTime()) {
$('.categoryName').hide();
} else if (current.getTime() < expiry.getTime()) {
$('.categoryName').show();
}
})
let curIndex = 0;
$.each(newCrs, function(idx, val) {
curIndex++; // this line must be here
let targetDiv = $("div.new-training-div > div[col='" + curIndex + "']");
let modalTrigger = $('<div />', {
'class': 'categoryName',
'data-category': val.Category,
'data-target': '#modal-id',
'data-toggle': 'modal',
'text': val.Category
});
modalTrigger.prepend("<span class='triangle-right'>▸</span>");
targetDiv.append(modalTrigger);
if(curIndex == 4) {
curIndex = 0;
}
})
} // ------------------ loadNewCourses
}
JSON snippet:
},
"FileSystemObjectType": 0,
"Id": 80,
"Title": "Rome",
"Category": "WorldCapitals",
"Description": "A capital city (or simply capital) is the municipality exercising primary status in a country, state, province, or other administrative region, usually as its seat of government.",
"TopTrainingCourse": false,
"VideoLink": "https:\/\/www.google.com",
"ID": 80,
"Modified": "2019-03-19T18:50:39Z",
"Created": "2019-03-19T18:50:39Z"
}
...etc