13

I'm working on a project that need to handle dates BC (eg 100BC, or 2000BC) as well as 2014AC etc..

In the API documentation of Datepicker, it is stated that minDate and maxDate can be set using javascript Date object (with has min and max of aprox 285,616 years on either side of 1970).

It seems to be impossible to set years in BC or even before 1/1/99.

$("#date").datepicker({
    dateFormat: 'dd-mm-yy',
    showButtonPanel: true,
    changeMonth: true,
    changeYear: true,
    yearRange: '0:2010',
    inline: true });

How can I surpass this limit?

5
  • Did you try setting minDate and maxDate and then updating yearRange accordingly? Commented Dec 11, 2013 at 0:56
  • Seems fine if you set the first year in the yearRange to something far enough back: jsfiddle.net/j08691/wLUpU
    – j08691
    Commented Dec 11, 2013 at 1:23
  • @j08691 even in your example, it is not possible to select the date 1/2/70 for example. Also, note that in the year range you set -3000 and the dropdown shows -987. There is clearly an issue here. Not to mention that if you leave the dropdown intact, it selects 2013 as date (select date, then click to edit and select again without using year dropdown.. oops!)
    – Odys
    Commented Dec 11, 2013 at 2:09
  • Possible duplicate: stackoverflow.com/questions/1122989/…
    – Casey Falk
    Commented Jul 28, 2014 at 17:54
  • Check out this answer: stackoverflow.com/questions/25846123/…
    – Twisty
    Commented Nov 8, 2019 at 2:19

3 Answers 3

0

You will need to implement a new datepicker yourself. 09/04/1752 does not exist.

http://www.genealogytoday.com/columns/everyday/030902.html

Try typing that date into the datepicker on jquery's website.

http://jqueryui.com/datepicker/

Cheers.

4
  • You should provide some evidence on why it isn't possible (as you imply). Also, I don't really care about this particular date, but thanks for pointing that out. Interesting story
    – Odys
    Commented Dec 11, 2013 at 2:23
  • 2
    I did. The first link clearly explains that "the British Isles and all the English colonies, including America, lost 11 days--September 3 through 13." Maybe you will be fortunate enough to find another calendar that accounts for this. Good luck. Clearly jQuery Datepicker does not take this into account, so any date before then will be off. If you type "cal 1752" on a unix terminal, you can see that the cal program accounts for this. that might be a place to start
    – jbangerter
    Commented Dec 11, 2013 at 2:26
  • 2
    I didn't address your other concern that you don't care about this date. I think you should, because all dates prior to it will be at least 11 days off. Therefore, your weekday names will not match. If you do not need those, then no worries.
    – jbangerter
    Commented Dec 11, 2013 at 2:30
  • 2
    I should also add that your locality might make a difference. "Though these days disappeared in English lands in 1752, a number had already vanished in other places--France in 1582, Austria in 1584, and Norway in 1700."
    – jbangerter
    Commented Dec 11, 2013 at 2:33
0

I think the root issue is a math/logic bug. Initially, when the year drop down appears, it shows 19, currently for the year 2019. Switch to 1492, all good. Switch to 666, all good. Switch to 121, all good. Switch to 99... uh oh it's now 1999.

You can test this here:

$(function() {
  $("#datepicker").datepicker({
    dateFormat: 'dd/mm/yy',
    showButtonPanel: true,
    changeMonth: true,
    changeYear: true,
    yearRange: 'c-22019:c+1',
    inline: true
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="datepicker"></p>

This causes an issue if you try to work your way back to anything under 100 AD, like -1, or 1 BC.

It looks like the logic that builds the year drop down needs some help. You can select a date in BC, yet it's just hard to get there from the range. Will need to research it a bit and update later.

Update

I was looking at yearRange with a different logic than what is used. To get desired results, it's best to work based on the Current Year format c-nn:c+nn.

$(function() {
  $("#datepicker").datepicker({
    dateFormat: 'dd/mm/yy',
    showButtonPanel: true,
    changeMonth: true,
    changeYear: true,
    yearRange: '-2000:2020',
    inline: true
  });
  
  var oldD = new Date();
  oldD.setYear(-1);
  
  $("#datepicker").datepicker("option", "defaultDate", oldD);
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<p>Date: <input type="text" id="datepicker"></p>

There is still a 0 to 99 issue that I think is related to Date assuming and adding the 19 when it gets a year passed to it. I think this can be fixed by using a Date object, yet this was not done in this part of the jQuery UI Code:

    // Year selection
    if ( !inst.yearshtml ) {
        inst.yearshtml = "";
        if ( secondary || !changeYear ) {
            html += "<span class='ui-datepicker-year'>" + drawYear + "</span>";
        } else {

            // determine range of years to display
            years = this._get( inst, "yearRange" ).split( ":" );
            thisYear = new Date().getFullYear();
            determineYear = function( value ) {
                var year = ( value.match( /c[+\-].*/ ) ? drawYear + parseInt( value.substring( 1 ), 10 ) :
                    ( value.match( /[+\-].*/ ) ? thisYear + parseInt( value, 10 ) :
                    parseInt( value, 10 ) ) );
                return ( isNaN( year ) ? thisYear : year );
            };
            year = determineYear( years[ 0 ] );
            endYear = Math.max( year, determineYear( years[ 1 ] || "" ) );
            year = ( minDate ? Math.max( year, minDate.getFullYear() ) : year );
            endYear = ( maxDate ? Math.min( endYear, maxDate.getFullYear() ) : endYear );
            inst.yearshtml += "<select class='ui-datepicker-year' data-handler='selectYear' data-event='change'>";
            for ( ; year <= endYear; year++ ) {
                inst.yearshtml += "<option value='" + year + "'" +
                    ( year === drawYear ? " selected='selected'" : "" ) +
                    ">" + year + "</option>";
            }
            inst.yearshtml += "</select>";

            html += inst.yearshtml;
            inst.yearshtml = null;
        }
    }
-2

You wouldn't be able to use B.C. with our current calendars anyway because they used the Julian calendar. Try converting our dates to Julian and see if it works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.