@atlaskit/datetime-picker
Version:
A date time picker allows the user to select an associated date and time.
50 lines (47 loc) • 2.26 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getParsedISO = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _dateFns = require("date-fns");
var _padTwo = require("./pad-two");
/**
* Everything in this file is to smooth out the migration of the new date picker
* (https://product-fabric.atlassian.net/browse/DSP-20682). When that ticket is
* complete, all of these functions will ilkely be merged back into the date
* picker. Please do not pre-optimize and put these back into the date picker
* unless you are working on the DTP Refresh and you have a good reason to do
* so, thank you!
*
* All variables within the `di` objects are dependency injections. They should
* be read from within the component at the end of the day. But because we are
* extracting them, we have to inject them in every place manually. When we
* re-introduce them to the components, we can likely remove the `di` variables
* and instead use internal variables.
*
* If component _only_ has injected variables, it is fully internal and was
* broken out to be it's own function.
*/
// oxlint-disable-next-line @atlassian/no-restricted-imports
var getParsedISO = exports.getParsedISO = function getParsedISO(di) {
var iso = di.iso;
var _iso$split = iso.split('-'),
_iso$split2 = (0, _slicedToArray2.default)(_iso$split, 3),
year = _iso$split2[0],
month = _iso$split2[1],
date = _iso$split2[2];
var newIso = iso;
var parsedDate = parseInt(date, 10);
var parsedMonth = parseInt(month, 10);
var parsedYear = parseInt(year, 10);
var lastDayInMonth = (0, _dateFns.lastDayOfMonth)(new Date(parsedYear, parsedMonth - 1 // This needs to be -1, because the Date constructor expects an index of the given month
)).getDate();
if (lastDayInMonth < parsedDate) {
newIso = "".concat(parsedYear, "-").concat((0, _padTwo.padToTwo)(parsedMonth), "-").concat((0, _padTwo.padToTwo)(lastDayInMonth));
} else {
newIso = "".concat(parsedYear, "-").concat((0, _padTwo.padToTwo)(parsedMonth), "-").concat((0, _padTwo.padToTwo)(parsedDate));
}
return newIso;
};