Misunderstanding or bug?

Misunderstanding or bug?

AlchetecAlchetec Posts: 14Questions: 5Answers: 0
edited February 2016 in Bug reports

I have the following field definition:

{
    "label": "To",
    "name": "PermissionToEnter.allow_to",
    "type": "datetime",
    "format": "MM-DD-YYYY",
    "def": function () { return new Date() },
    "opts": {
        "firstDay" : 0,
        "minDate": new Date()
    }
},

The default date is correct, but the minDate does not seem to be working i.e. I cannot select the current date. I can select tomorrow but not today.

I am I misunderstanding how things should work of is this a bug?

Thanks!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    The issue if the new Date() gives the date and the time - i.e. it might be 29th Feb 2016 13:45:10, but because the calendar aspect of the datetime field uses just the date part if you add the time then it appears to be greater - hence why it won't let you select that date.

    What you probably need to do is something like:

    minDate: (function () {
      var d = new Date();
      d.setUTCHours(0);
      d.setUTCMinutes(0);
      d.setUTCSeconds(0);
      d.setUTCMilliseconds(0);
      return d;
    }())
    

    Allan

  • AlchetecAlchetec Posts: 14Questions: 5Answers: 0

    So I did as you showed but things where funky so I did the following:

    minDate: ( function () {
      var d = new Date();
      d.setHours(0);
      d.setMinutes(0);
      d.setSeconds(0);
      d.setMilliseconds(0);
      return d;
    }())
    

    Which kind of worked BUT the current date when the calendar pops up shows tomorrow. I did this at 2016-03-04 @ 14:50 but the calendar shows its the 5th.

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin

    Could you give me a link to the page show I can debug it please? Also, what time zone are you in?

    Allan

  • AlchetecAlchetec Posts: 14Questions: 5Answers: 0

    Sorry for the delay, I am in the Central time zone. I sent you the page link in an email. Please let me know if you did not receive it. Thanks!

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Super - thanks for the information and also the PM with the link. The error is in how Editor calculates today's date - its wrong for timezones with a negative UTC offset. I've committed the fix and it will be in Editor 1.5.6 which I expect to be available early / mid April.

    In the mean time, if you'd like to make the change locally, search the Editor code for:

        _compareDates: function( a, b ) {
            return a.toDateString() === b.toDateString();
        },
    

    And replace with:

        _compareDates: function( a, b ) {
            // Can't use toDateString as that converts to local time
            return this._dateToUtcString(a) === this._dateToUtcString(b);
        },
    
        _dateToUtcString: function ( d ) {
            return d.getUTCFullYear()+'-'+
                this._pad(d.getUTCMonth()+1)+'-'+
                this._pad(d.getUTCDate());
        },
    

    The problem was that toDateString() uses local time, while Editor's date handling is in UTC as much as possible.

    Regards,
    Allan

This discussion has been closed.