0

I am trying to show some data within calendar dates. I giving it a start date and an end date. I only want it to show within those dates. I have been trying to do this but I cannot seem to make it work right. I thought I had it but was wrong.

var objData = [];
var month = [ "january","february","march", "april", "may", "june", "july", "august", "september", "October", "november", "december"];
var d = new Date( );
var currDate = d.getDate();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
var currPromoIndex;

for (var i =0; i < obj.length; i++)
{
    var start = obj[i].dateRange[0];
    var end = obj[i].dateRange[1];
    var promoStartMonth = start.slice( 0,2 ).replace("/", "");
    var promoStartDay = start.slice( start.indexOf("/"), start.length ).replace("/", "");
    var promoEndMonth = end.slice( 0,2 ).replace("/", "");
    var promoEndDay = end.slice( end.indexOf("/"), end.length ).replace("/", "");

    var myPromoStart = new Date( currYear, promoStartMonth-1, promoStartDay);
    var myPromoEnd = new Date( currYear, promoEndMonth-1, promoEndDay);

    if ( ( ( d > myPromoStart ) && ( d < myPromoEnd) ) || ( d >= myPromoEnd ) ) 
    {
        objData = obj[i];
        console.log(obj[i]);
    } 
}

My Date Range is stored in a json and it looks like this.

"dateRange" : [ "4/13", "5/12" ],...

Im not sure how else to do this as I have tried many different ways and nothing seems to be working accordingly.

1
  • what is this for? ( d >= myPromoEnd ) Commented Feb 22, 2016 at 5:07

2 Answers 2

2

It's unclear what the data object actually looks like, the "JSON" is not valid.

You seem to have made a good start, but are getting lost in converting the range to dates.

var objData = [];
var month = [ "january","february","march", "april", "may", "june", "july", "august", "september", "October", "november", "december"];

The month variable isn't used, so it's not necessary. The next part seems OK:

var d = new Date( );
var currDate = d.getDate();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
var currPromoIndex;

for (var i =0; i < obj.length; i++) {

I'll assume that obj[i].dateRange is ["4/13", "5/12"].

    var start = obj[i].dateRange[0];
    var end = obj[i].dateRange[1];
    var promoStartMonth = start.slice( 0,2 ).replace("/", "");
    var promoStartDay = start.slice( start.indexOf("/"), start.length ).replace("/", "");

Extracting the month and day from the range values is simpler if you use a regular expression and split:

    var start = obj[i].dateRange[0].split(/\D/);
    var end = obj[i].dateRange[1].split(/\D/);
    var promoStartMonth = start[0];
    var promoStartDay = start[1];
    var promoEndMonth = end[0];
    var promoEndDay = end[1];

The next bit is OK:

    var myPromoStart = new Date( currYear, promoStartMonth-1, promoStartDay);
    var myPromoEnd = new Date( currYear, promoEndMonth-1, promoEndDay);

But the logic here is flawed (and has too many parenthesis for easy reading):

    if (((d > myPromoStart) && (d < myPromoEnd)) || (d >= myPromoEnd)) {

Why the ||? That will include all dates after myPromoEnd. If you want the range to include the end dates, then use (and remove most of the parenthesis):

    if (d >= myPromoStart && d <= myPromoEnd) {

And then you have an issue with:

        objData = obj[i];

That replaces the value of objData with obj[i], you probably want to add it to the array, so:

        objData.push(obj[i]);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this for for (var i =0; i < Object.keys(obj).length ; i++) You cant get the size of an object just using .length. and what RobG said, you need to use .push() to add an object to the array. Hope this help:

https://jsfiddle.net/geradrum/c2xkntsh/

2 Comments

These are both great answers but for some reason once it gets to december it breaks
here is my complete json '"promotions": [ { "id": "winter", "dateRange" : [ "12/1", "1/13" ], "title":"Cut Ahead At The Riviera", "desc":"providing you personalized care", "body": "book.php", "color": "#3690ce", "backgroundImage": "backgroundImage.svg", "background": "background.svg", "mainImage": "main.svg" } ]' this is one example. All the other months are in the same format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.