I have an Object of Objects formatted like so:
var data = {
09-29-2017: Array[
{jobNumber: "59067", jobState: "FinishedGoods"},
{jobNumber: "59068", jobState: "inManufacturing"}
],
09-08-2017: Array[
{jobNumber: "62155", jobState: "Approved"}]
... continued for a lot of dates and anywhere between 0-50 jobNumber and corresponding jobStates per date.
our weeks are set on fridays so every date is a friday and that is all the jobs for the preceding week.
using javascript, jquery, moment.js, or any other suggested Library I'm trying to write a function that I can pass the date from the data object to and return an array with all of the dates that have at least one Job in them that is greater or equal to the closest friday.
So far I've come up with this:
function getWeeksWithJobs(){
var data = getData(); //This is the object of arrays of objects that I'm trying to pull the date from
var today = new Date();
today = moment(today).format('MM-DD-YYYY');
var weekRay = []; // this is the array I'm trying to return with just the dates that fit the specifications
$.each(data, function(index, value){ //in this function index returns the date, and the value returns the array containing the objects.
if (data[index].length > 0) // this seems to be working fine
//I'm just stuck here with how to compare the dates
//If I use moment(index).format('MM-DD-YYYY') it throws an error
//If I just use console.log(index) it outputs the date
// in the right format and everything no punctuation or anything
// If I just compare them normally index >= today it just
// compares the month.
weekRay.push(index);
});
return weekRay;
}
My biggest issue is just trying to make the dates comparable, they are both in the same format 'MM-DD-YYYY' but they don't compare. I welcome any advice, thank you in advance.
EDIT: I found something that works, if I converted the dates into a moment object using today = moment(today, 'MM-DD-YYYY') then I passed them to a function that converts the date to an epochValue:
function epochDays(date)
{
date = new Date(date) // and then convert them back into a Date object
return Math.floor(date /day_mSecs); //const day_mSecs =86400000
}
So I had to take the variables convert them to a moment() object, then from there convert them to a Date object before I could even do much with them. They were finally comparable and I was able to find which days came after the current date. Thanks for all your help everybody.
Please try the below code to compare the date.
var today = new Date();
today.setHours(0,0,0,0);
$.each(data, function(index, value){
if (data[index].length > 0){
// if index returns the date than
var newIndex = new Date(index);
newIndex .setHours(0,0,0,0);
// now compare the both dates
if(newIndex >= today)
{
// TO DO
}
weekRay.push(index);
}
});
You need to provide format argument when passing in non standard date strings to moment():
Example:
moment("12-25-1995", "MM-DD-YYYY");
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