UNPKG

react-native-nepali-picker

Version:

Minimalist and modern Nepali-date picker with customization.🌟

163 lines (155 loc) • 5.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateDate = exports.formatDate = exports.NepaliToday = exports.FindDateDifference = exports.BsToAd = exports.AdToBs = void 0; var _config = require("./config.js"); var _settings = require("./settings.js"); const formatDate = date => { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }; // Add padart 0 if the date is day or months are denoted by single number exports.formatDate = formatDate; const normalizeNepaliDate = date => { const parts = date.split('-'); if (parts.length !== 3) return null; const [y, m, d] = parts; if (!/^\d{4}$/.test(y)) return null; if (!/^\d{1,2}$/.test(m)) return null; if (!/^\d{1,2}$/.test(d)) return null; return `${y}-${m.padStart(2, '0')}-${d.padStart(2, '0')}`; }; const validateDate = uDate => { const normalized = normalizeNepaliDate(uDate); if (!normalized) { return 'Date format must be YYYY-MM-DD'; } // format check if (!/^\d{4}-\d{2}-\d{2}$/.test(normalized)) { return 'Date format must be YYYY-MM-DD'; } const [yearStr, monthStr, dayStr] = normalized.split('-'); const year = Number(yearStr); const month = Number(monthStr); const day = Number(dayStr); // Year range if (year < _settings.NEPALI_MIN_YEAR || year > _settings.NEPALI_MAX_YEAR) { return 'Year range must be between 2000 and 2099 (BS)'; } // Month range if (month < 1 || month > 12) { return 'Month range must be between 01 and 12'; } //Day range using BS data const daysInMonth = _config.bs?.[year]?.[month]; if (!daysInMonth) { return 'Invalid Nepali calendar data'; } if (day < 1 || day > daysInMonth) { return `Day range for ${year}-${monthStr} is 01 to ${daysInMonth}`; } return true; }; exports.validateDate = validateDate; const FindDateDifference = (date1, date2) => { const diffInMs = date2 - date1; const diffInDays = diffInMs / (1000 * 60 * 60 * 24); return diffInDays; }; exports.FindDateDifference = FindDateDifference; const AdToBs = UserDate => { const ReferenceDate = new Date('1943-04-14').getTime(); const UserTimeDate = new Date(UserDate).getTime(); if (UserTimeDate < ReferenceDate) { console.log('The minimum possible date you can enter is 2024-04-13 i.e 2081-01-01'); return 'The minimum possible date you can enter is 2024-04-13 i.e 2081-01-01'; } // number of days from that reference date const DateDifference = FindDateDifference(ReferenceDate, UserTimeDate); let nepaliYear = _settings.NEPALI_MIN_YEAR; let nepaliMonth = 1; let nepaliDay = 1; //difference can calculate upto previous day so add 1 to get current day(Today) let DD = DateDifference + 1; outerLoop: for (let year = _settings.NEPALI_MIN_YEAR; year <= _settings.NEPALI_MAX_YEAR; year++) { for (let month = 1; month <= 12; month++) { if (DD <= _config.bs[year][month]) { nepaliYear = year; nepaliMonth = month; nepaliDay = DD; break outerLoop; } else { DD -= _config.bs[year][month]; } } } return `${nepaliYear}-${String(nepaliMonth).padStart(2, '0')}-${String(nepaliDay).padStart(2, '0')}`; }; exports.AdToBs = AdToBs; const NepaliToday = () => { const ReferenceDate = new Date('1943-04-14').getTime(); const TodayDate = Date.now(); const date = new Date(TodayDate); // number of return from that reference date const DateDifference = FindDateDifference(ReferenceDate, new Date(formatDate(date)).getTime()); let nepaliYear = _settings.NEPALI_MIN_YEAR; let nepaliMonth = 1; let nepaliDay = 1; //difference can calculate upto previous day so add 1 to get current day(Today) let DD = DateDifference + 1; outerLoop: for (let year = _settings.NEPALI_MIN_YEAR; year <= _settings.NEPALI_MAX_YEAR; year++) { for (let month = 1; month <= 12; month++) { if (DD <= _config.bs[year][month]) { nepaliYear = year; nepaliMonth = month; nepaliDay = DD; break outerLoop; } else { DD -= _config.bs[year][month]; } } } return `${nepaliYear}-${String(nepaliMonth).padStart(2, '0')}-${String(nepaliDay).padStart(2, '0')}`; // return DateDifference; //add the difference between reference and today date to nepali reference date and find new today nepali date }; exports.NepaliToday = NepaliToday; const BsToAd = userDate => { try { const dateArray = userDate.split('-'); let dateDifference = 0; if (dateArray.length !== 3) { throw new Error('Invalid date format'); } const [userYear, userMonth, userDay] = dateArray.map(Number); if (userYear < _settings.NEPALI_MIN_YEAR || userYear > _settings.NEPALI_MAX_YEAR) { throw new Error('Year Range is 2000 to 2099'); } if (userMonth < 1 || userMonth > 12) { throw new Error('Month Range is 1 to 12'); } // if (userMonth < 1 || userMonth > 31) { // throw new Error('Month out of supported range'); // } for (let year = _settings.NEPALI_MIN_YEAR; year < userYear; year++) { for (let month = 1; month <= 12; month++) { dateDifference += _config.bs[year][month]; } } for (let month = 1; month < userMonth; month++) { dateDifference += _config.bs[userYear][month]; } dateDifference += userDay - 1; const referenceDate = new Date('1943-04-14'); const futureDate = new Date(referenceDate); futureDate.setDate(referenceDate.getDate() + dateDifference); return futureDate.toISOString().split('T')[0]; } catch (error) { return ' Year Range is 2000 to 2099'; } }; exports.BsToAd = BsToAd; //# sourceMappingURL=functions.js.map