amsul / pickadate.js

The mobile-friendly, responsive, and lightweight jQuery date & time input picker.
http://amsul.ca/pickadate.js
MIT License
7.7k stars 1.02k forks source link

Cannot set date to year 0001 #1154

Closed twobyte closed 5 years ago

twobyte commented 5 years ago

We have been using a hack to filter the date field so that we can use outlandish dates such as 9999/01/01 and 0001/01/01 to represent meta such as “Open Ended” and “Not Applicable”.

This has been working fine when editing the date as a text field, but now we are integrating datepicker.js to improve usability and everytime I run picker.set( 'select', '0001-01-01', { format: 'yyyy-mm-dd' } ); the date is set to 1st January 1901.

Other dates work, such as 9999-01-01 (1st January 9999) and 1001-01-01 (1st January 1001), but for some reason it wont accept year 0001.

Is this a bug or a mis-configuration issue? Would be nice to work with the existing data rather than having to run some global database operation to change this value site-wide.

Thanks

DanielRuf commented 5 years ago

Does -Infinity and Infinity not work for you? https://github.com/amsul/pickadate.js/blob/3.6.0/lib/picker.date.js#L215 https://github.com/amsul/pickadate.js/blob/3.6.0/lib/picker.date.js#L407

DanielRuf commented 5 years ago

This is because the string is casted to an integer / parsed as integer. See new Date(0001, 01, 01) which results in Fri Feb 01 1901 00:00:00

twobyte commented 5 years ago

Thanks for the quick reply @DanielRuf. I am sorry but is Infinity a date?

DanielRuf commented 5 years ago

Well, see https://amsul.ca/pickadate.js/date/#limits-relative https://codepen.io/DanielRuf/pen/mgRwjj

DanielRuf commented 5 years ago

Either you use a year with starts with 1 or set needed properties and read them. Like the min and max values.

twobyte commented 5 years ago

Thanks Daniel, just added { max: false, min: false } to the pickadate options but it still rendering date as 1901 rather than 0001. Would this not be classed as a bug as I am specifying the date format so why still casting to an integer?

DanielRuf commented 5 years ago

Not sure what you mean but yyyy uses Date and parses it.

0000...1 makes not much sense when you work with integers and dates.

twobyte commented 5 years ago

This is quite a good article about the issues specifying year 1 with the Date object.

Running console.log((new Date('0001-01-01')).toUTCString()) in browser is correctly returning "Mon, 01 Jan 0001 00:00:00 GMT" – but still I cannot get pickadate.js to correctly select this date.

I have tried passing the date in as both a Date object and an array, but still returning 1901:

targetData.value = '0001-01-01';
picker.set( 'select', targetData.value { format: 'yyyy-mm-dd' } );

returns 1901-01-01

targetData.value = '0001-01-01';
var dateArray = targetData.value.split('-');
picker.set( 'select', new Date(dateArray[0], dateArray[1]-1, dateArray[2]));

Still returns date as '1901-01-01'

targetData.value = '0001-01-01';
var dateArray = targetData.value.split('-');
picker.set( 'select', [dateArray[0], dateArray[1]-1, dateArray[2]]);

'1901-01-01' again...

How can I force datepicker to not change the year to an integer and assume 1900?

DanielRuf commented 5 years ago

How can I force datepicker to not change the year to an integer and assume 1900?

Currently not possibly except you overwrite it (using proto or some other way).