jtsage / jtsage-datebox

A multi-mode date and time picker for Bootstrap (3&4), jQueryMobile, Foundation, Bulma, FomanticUI, and UIKit (or others)
http://datebox.jtsage.dev/
Other
474 stars 166 forks source link

Refactor offset.js #442

Closed jtsage closed 5 years ago

jtsage commented 5 years ago

It's a huge unreadable mess

jtsage commented 5 years ago

In progress.

Edge case that I need help thinking through.

Date rollover is disallowed.

I.e. May 31 + 1 day == May 1, not June 1.

Date is set to May 31, subtract 1 month. Result is April 31, which does not exist, so it becomes May 1.

What should happen there? April 30? Of course, this gets worse for February.

Thoughts appreciated.

slavap commented 5 years ago

@jtsage Not sure I'm following the logic. If rollover is disallowed, why May 31 subtract 1 month should become April?

jtsage commented 5 years ago

It's a difference in the number of the days of the month.

I.E.

May 15 -> Offset to 1 month prior -> April 15.

May 31 -> Offset to 1 month prior -> DateBox doesn't know what to do 

Option 1:

Leave it be, and understand there may be inconsistency near the end of the month. This is what browsers do, but then, the browser implementation has no concept of not allowing the rollover.

Option 2:

Attempt to normalize it by setting date to the last day of the month. In this example:

May 31 -> Offset 1 month prior -> April 30.

or

January 31 -> Offset 1 month next -> February 28 (or 29, depending on the year).

It works out to be a problem for something like 10 days out of the year.

slavap commented 5 years ago

@jtsage I think it could be solved like this, minor performance hit, but quite bulletproof 😁 https://jsfiddle.net/7h0okygL/

jtsage commented 5 years ago

Cool, we are on the same page as to how it should feel.

I'm even putting in a short bit to handle changing the year when set to February 29th. (Not going to do a lot of checks on this one, just drop it back to the 28th.