0

I need to compare dates with javascript so I write a function:

function fulljson (){
    var db_data;
    $.ajax({
                url: "http://localhost:8888/auction/offers/5", 
                type: "GET",
                async: true, 
                dataType: "html",
                success: function(data) {
                var db_data = $.parseJSON(data);
                console.log(db_data);

    // declare variables
    var period_start = new Date('2016-02-24'),
        period_now = new Date();
        period_end = new Date('2016-11-01'),
        //current_date = period_start,
        array_of_all_dates = [];
console.log(period_start);
        var dodaj = parseInt('3');
        period_now = period_now.setDate(period_now.getDate() + dodaj);

        console.log(new Date(period_start).getTime() + ' i ' + period_now);

        if (new Date(period_start).getTime() > period_now){
            current_date = new Date(period_start);

        } else {
            current_date = new Date(period_now);
            current_date.setHours(1
                ,0,0,0);
        }

        //current_date = moment(current_date).format('YYYY-MM-DD');
        console.log(current_date);
    // Create a populated array of dates
   // Create a populated array of dates
    while (current_date.getTime() <= period_end.getTime()) {
      array_of_all_dates.push(current_date);
      current_date = new Date(+current_date);
      current_date.setDate(current_date.getDate() + 1);
    }

    // Now loop over the array of populated dates and mutate, so something like
    array_of_all_dates = array_of_all_dates.map(function (date) {
      var found_in_db = db_data.filter(function (db_data) {
        return new Date(db_data.start).getTime() === date.getTime(); // You need to do this comparison better!
      });
      if (found_in_db.length > 0) {
        return found_in_db[0];
      }
      var new_object = {
        title: '',
        start: date,
        price: '60'
      };
      console.log(new_object);
      return new_object;

    });



    console.log('result'+array_of_all_dates);
    drawCalendar(array_of_all_dates);
                }, 
                error: function (data) {
                console.log(data);
                console.log('GRESKA NEKA');
                }      
    });
        //end OF AJAX

};

My ajax function gets this data:

[{"id":82,"price":61,"start":"Mon, 28 Mar 2016 00:00:00 +0000"},{"id":81,"price":61.5,"start":"Sun, 27 Mar 2016 00:00:00 +0000"},{"id":79,"price":61,"start":"Sun, 13 Mar 2016 00:00:00 +0000"},{"id":72,"price":61,"start":"Tue, 29 Mar 2016 00:00:00 +0000"},{"id":66,"price":61,"start":"Sat, 12 Mar 2016 00:00:00 +0000"},{"id":64,"price":60.5,"start":"Fri, 11 Mar 2016 00:00:00 +0000"}]

Now my function which needs to compare dates works only in this case to 27. March 2016.

Also my period_start is 24. Feb. and period_end is 1.Nov.2016.

Why my function work only to 27.March?

11
  • Is this some PHP templating system? new Date('{{ date('Y-m-d', strtotime($article->from)) }}')? JS has no strtotime() or date() function Commented Feb 10, 2016 at 20:10
  • Yes its Blade - Laravel... Commented Feb 10, 2016 at 20:10
  • show the actual generated JS, then. not this js/php mishmash. Commented Feb 10, 2016 at 20:11
  • sorry,ok - I update my question... Commented Feb 10, 2016 at 20:13
  • you really should evaluate that code again. why are you doing *THREE new Date(period_start) in a row? why create the same object three times, especially since you don't change the date value... Commented Feb 10, 2016 at 20:15

1 Answer 1

1

This is a comment, but there's not enough space and I'd like to format it.

In the code:

var period_start = new Date('2016-02-24'),
        period_now = new Date();  // <=== terminates statement
        period_end = new Date('2016-11-01'),
        //current_date = period_start,
        array_of_all_dates = [];

Note that the second line is terminated with a semicolon, so period_end and array_of_all_dates become global. This likely isn't an issue, but who knows…

Also, the current interpretation of ECMAScript 2015 is that:

new Date('2016-02-24')

should create a new Date for 2016-02-24T00:00:00Z, i.e. it is treated as UTC (which is inconsistent with ISO 8601 and the standard itself since a date and time string without a timezone like 2016-02-24T00:00:00 is treated as local, but remove the time part and it's UTC).

Next,

new Date()

creates a local* date, so if today is 2016-02-12 then:

new Date() != new Date('2016-02-12')

for all time zones with an offset other than 00:00 since the time values will differ by the timezone offset.

It's not clear to me whether the above has a serious affect on your code or not, but freely mixing UTC and local dates is not a good idea.

Next, I don't understand the use of:

var dodaj = parseInt('3');

rather than:

var dodaj = 3;

And:

if (new Date(period_start).getTime() > period_now)

creates and discards a Date object for no reason, the following is exactly equivalent:

if (period_start > period_now)

with the benefit that it avoids an obscure bug in IE regarding new Date(date) and two digit years.

Oh, lastly, 27 March 2016 is a Sunday when daylight saving starts in many places (e.g. Italy, though not the US), so perhaps daylight saving is messing with your dates?

* where "local date" means a Date with a time value adjusted for the current timezone offset settings of the host.

Sign up to request clarification or add additional context in comments.

4 Comments

I solve my problem with: return new Date(db_data.start).toDateString() === date.toDateString();
@MonkeyBusiness—it would make more sense to use return +new Date(db_data.start) == +date so that you compare the time values directly.
Can you please write full example and we can test?
@MonkeyBusiness—once there is sufficient information to do so. Are the dates to be treated as local or UTC?

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.