var calendarFunctions = {}; !(function ($) { var calendarData = { bsMonths: [ "बैशाख", "जेठ", "असार", "साउन", "भदौ", "असोज", "कार्तिक", "मंसिर", "पौष", "माघ", "फागुन", "चैत", ], bsDays: ["आइत", "सोम", "मंगल", "बुध", "बिही", "शुक्र", "शनि"], nepaliNumbers: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], bsMonthUpperDays: [ [30, 31], [31, 32], [31, 32], [31, 32], [31, 32], [30, 31], [29, 30], [29, 30], [29, 30], [29, 30], [29, 30], [30, 31], ], extractedBsMonthData: [ [ 0, 1, 1, 22, 1, 3, 1, 1, 1, 3, 1, 22, 1, 3, 1, 3, 1, 22, 1, 3, 1, 19, 1, 3, 1, 1, 3, 1, 2, 2, 1, 3, 1, ], [ 1, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 3, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 3, 1, 1, 2, ], [ 0, 1, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 3, 1, 1, 2, ], [ 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 2, 2, 1, 3, 1, 2, 2, 2,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 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 20, 3, ], ], minBsYear: 1970, maxBsYear: 2100, minAdDateEqBsDate: { ad: { year: 1913, month: 3, date: 13 }, bs: { year: 1970, month: 1, date: 1 }, }, }, validationFunctions = { validateRequiredParameters: function (requiredParameters) { $.each(requiredParameters, function (key, value) { if ("undefined" == typeof value || null === value) throw new ReferenceError( "Missing required parameters: " + Object.keys(requiredParameters).join(", ") ); }); }, validateBsYear: function (bsYear) { if ("number" != typeof bsYear || null === bsYear) throw new TypeError("Invalid parameter bsYear value"); if (bsYear < calendarData.minBsYear || bsYear > calendarData.maxBsYear) throw new RangeError( "Parameter bsYear value should be in range of " + calendarData.minBsYear + " to " + calendarData.maxBsYear ); }, validateAdYear: function (adYear) { if ("number" != typeof adYear || null === adYear) throw new TypeError("Invalid parameter adYear value"); if ( adYear < calendarData.minBsYear - 57 || adYear > calendarData.maxBsYear - 57 ) throw new RangeError( "Parameter adYear value should be in range of " + (calendarData.minBsYear - 57) + " to " + (calendarData.maxBsYear - 57) ); }, validateBsMonth: function (bsMonth) { if ("number" != typeof bsMonth || null === bsMonth) throw new TypeError("Invalid parameter bsMonth value"); if (bsMonth < 1 || bsMonth > 12) throw new RangeError( "Parameter bsMonth value should be in range of 1 to 12" ); }, validateAdMonth: function (adMonth) { if ("number" != typeof adMonth || null === adMonth) throw new TypeError("Invalid parameter adMonth value"); if (adMonth < 1 || adMonth > 12) throw new RangeError( "Parameter adMonth value should be in range of 1 to 12" ); }, validateBsDate: function (bsDate) { if ("number" != typeof bsDate || null === bsDate) throw new TypeError("Invalid parameter bsDate value"); if (bsDate < 1 || bsDate > 32) throw new RangeError( "Parameter bsDate value should be in range of 1 to 32" ); }, validateAdDate: function (adDate) { if ("number" != typeof adDate || null === adDate) throw new TypeError("Invalid parameter adDate value"); if (adDate < 1 || adDate > 31) throw new RangeError( "Parameter adDate value should be in range of 1 to 31" ); }, validatePositiveNumber: function (numberParameters) { $.each(numberParameters, function (key, value) { if ("number" != typeof value || null === value || value < 0) throw new ReferenceError( "Invalid parameters: " + Object.keys(numberParameters).join(", ") ); if ( "yearDiff" === key && value > calendarData.maxBsYear - calendarData.minBsYear + 1 ) throw new RangeError( "Parameter yearDiff value should be in range of 0 to " + (calendarData.maxBsYear - calendarData.minBsYear + 1) ); }); }, }; $.extend(calendarFunctions, { getNepaliNumber: function (number) { if ("undefined" == typeof number) throw new Error("Parameter number is required"); if ("number" != typeof number || number < 0) throw new Error("Number should be positive integer"); var prefixNum = Math.floor(number / 10), suffixNum = number % 10; return 0 !== prefixNum ? calendarFunctions.getNepaliNumber(prefixNum) + calendarData.nepaliNumbers[suffixNum] : calendarData.nepaliNumbers[suffixNum]; }, getNumberByNepaliNumber: function (nepaliNumber) { if ("undefined" == typeof nepaliNumber) throw new Error("Parameter nepaliNumber is required"); if ("string" != typeof nepaliNumber) throw new Error("Parameter nepaliNumber should be in string"); for (var number = 0, i = 0; i < nepaliNumber.length; i++) { var numIndex = calendarData.nepaliNumbers.indexOf( nepaliNumber.charAt(i) ); if (numIndex === -1) throw new Error("Invalid nepali number"); number = 10 * number + numIndex; } return number; }, getBsMonthInfoByBsDate: function ( bsYear, bsMonth, bsDate, dateFormatPattern ) { if ( (validationFunctions.validateRequiredParameters({ bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate, }), validationFunctions.validateBsYear(bsYear), validationFunctions.validateBsMonth(bsMonth), validationFunctions.validateBsDate(bsDate), null === dateFormatPattern) ) dateFormatPattern = "%D, %M %d, %y"; else if ("string" != typeof dateFormatPattern) throw new TypeError("Invalid parameter dateFormatPattern value"); var daysNumFromMinBsYear = calendarFunctions.getTotalDaysNumFromMinBsYear( bsYear, bsMonth, bsDate ), adDate = new Date( calendarData.minAdDateEqBsDate.ad.year, calendarData.minAdDateEqBsDate.ad.month, calendarData.minAdDateEqBsDate.ad.date - 1 ); adDate.setDate(adDate.getDate() + daysNumFromMinBsYear); var bsMonthFirstAdDate = calendarFunctions.getAdDateByBsDate( bsYear, bsMonth, 1 ), bsMonthDays = calendarFunctions.getBsMonthDays(bsYear, bsMonth); bsDate = bsDate > bsMonthDays ? bsMonthDays : bsDate; var eqAdDate = calendarFunctions.getAdDateByBsDate( bsYear, bsMonth, bsDate ), weekDay = eqAdDate.getDay() + 1, formattedDate = calendarFunctions.bsDateFormat( dateFormatPattern, bsYear, bsMonth, bsDate ); return { bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate, weekDay: weekDay, formattedDate: formattedDate, adDate: eqAdDate, bsMonthFirstAdDate: bsMonthFirstAdDate, bsMonthDays: bsMonthDays, }; }, getAdDateByBsDate: function (bsYear, bsMonth, bsDate) { validationFunctions.validateRequiredParameters({ bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate, }), validationFunctions.validateBsYear(bsYear), validationFunctions.validateBsMonth(bsMonth), validationFunctions.validateBsDate(bsDate); var daysNumFromMinBsYear = calendarFunctions.getTotalDaysNumFromMinBsYear( bsYear, bsMonth, bsDate ), adDate = new Date( calendarData.minAdDateEqBsDate.ad.year, calendarData.minAdDateEqBsDate.ad.month, calendarData.minAdDateEqBsDate.ad.date - 1 ); return adDate.setDate(adDate.getDate() + daysNumFromMinBsYear), adDate; }, getTotalDaysNumFromMinBsYear: function (bsYear, bsMonth, bsDate) { if ( (validationFunctions.validateRequiredParameters({ bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate, }), validationFunctions.validateBsYear(bsYear), validationFunctions.validateBsMonth(bsMonth), validationFunctions.validateBsDate(bsDate), bsYear < calendarData.minBsYear || bsYear > calendarData.maxBsYear) ) return null; for ( var daysNumFromMinBsYear = 0, diffYears = bsYear - calendarData.minBsYear, month = 1; month <= 12; month++ ) daysNumFromMinBsYear += month < bsMonth ? calendarFunctions.getMonthDaysNumFormMinBsYear( month, diffYears + 1 ) : calendarFunctions.getMonthDaysNumFormMinBsYear(month, diffYears); return (daysNumFromMinBsYear += bsYear > 2085 && bsYear < 2088 ? bsDate - 2 : 2085 === bsYear && bsMonth > 5 ? bsDate - 2 : bsYear > 2088 ? bsDate - 4 : 2088 === bsYear && bsMonth > 5 ? bsDate - 4 : bsDate); }, getMonthDaysNumFormMinBsYear: function (bsMonth, yearDiff) { validationFunctions.validateRequiredParameters({ bsMonth: bsMonth, yearDiff: yearDiff, }), validationFunctions.validateBsMonth(bsMonth), validationFunctions.validatePositiveNumber({ yearDiff: yearDiff }); var yearCount = 0, monthDaysFromMinBsYear = 0; if (0 === yearDiff) return 0; for ( var bsMonthData = calendarData.extractedBsMonthData[bsMonth - 1], i = 0; i < bsMonthData.length; i++ ) if (0 !== bsMonthData[i]) { var bsMonthUpperDaysIndex = i % 2; if (!(yearDiff > yearCount + bsMonthData[i])) { (monthDaysFromMinBsYear += calendarData.bsMonthUpperDays[bsMonth - 1][ bsMonthUpperDaysIndex ] * (yearDiff - yearCount)), (yearCount = yearDiff - yearCount); break; } (yearCount += bsMonthData[i]), (monthDaysFromMinBsYear += calendarData.bsMonthUpperDays[bsMonth - 1][ bsMonthUpperDaysIndex ] * bsMonthData[i]); } return monthDaysFromMinBsYear; }, getBsMonthDays: function (bsYear, bsMonth) { validationFunctions.validateRequiredParameters({ bsYear: bsYear, bsMonth: bsMonth, }), validationFunctions.validateBsYear(bsYear), validationFunctions.validateBsMonth(bsMonth); for ( var yearCount = 0, totalYears = bsYear + 1 - calendarData.minBsYear, bsMonthData = calendarData.extractedBsMonthData[bsMonth - 1], i = 0; i < bsMonthData.length; i++ ) if (0 !== bsMonthData[i]) { var bsMonthUpperDaysIndex = i % 2; if (((yearCount += bsMonthData[i]), totalYears <= yearCount)) return (2085 === bsYear && 5 === bsMonth) || (2088 === bsYear && 5 === bsMonth) ? calendarData.bsMonthUpperDays[bsMonth - 1][ bsMonthUpperDaysIndex ] - 2 : calendarData.bsMonthUpperDays[bsMonth - 1][ bsMonthUpperDaysIndex ]; } return null; }, getBsDateByAdDate: function (adYear, adMonth, adDate) { validationFunctions.validateRequiredParameters({ adYear: adYear, adMonth: adMonth, adDate: adDate, }), validationFunctions.validateAdYear(adYear), validationFunctions.validateAdMonth(adMonth), validationFunctions.validateAdDate(adDate); var bsYear = adYear + 57, bsMonth = (adMonth + 9) % 12; bsMonth = 0 === bsMonth ? 12 : bsMonth; var bsDate = 1; if (adMonth < 4) bsYear -= 1; else if (4 === adMonth) { var bsYearFirstAdDate = calendarFunctions.getAdDateByBsDate( bsYear, 1, 1 ); adDate < bsYearFirstAdDate.getDate() && (bsYear -= 1); } var bsMonthFirstAdDate = calendarFunctions.getAdDateByBsDate( bsYear, bsMonth, 1 ); if (adDate >= 1 && adDate < bsMonthFirstAdDate.getDate()) { bsMonth = 1 !== bsMonth ? bsMonth - 1 : 12; var bsMonthDays = calendarFunctions.getBsMonthDays(bsYear, bsMonth); bsDate = bsMonthDays - (bsMonthFirstAdDate.getDate() - adDate) + 1; } else bsDate = adDate - bsMonthFirstAdDate.getDate() + 1; return { bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate }; }, getBsYearByAdDate: function (adYear, adMonth, adDate) { validationFunctions.validateRequiredParameters({ adYear: adYear, adMonth: adMonth, adDate: adDate, }), validationFunctions.validateAdYear(adYear), validationFunctions.validateAdMonth(adMonth), validationFunctions.validateAdDate(adDate); var bsDate = calendarFunctions.getBsDateByAdDate(adYear, adMonth, adDate); return bsDate.bsYear; }, getBsMonthByAdDate: function (adYear, adMonth, adDate) { validationFunctions.validateRequiredParameters({ adYear: adYear, adMonth: adMonth, adDate: adDate, }), validationFunctions.validateAdYear(adYear), validationFunctions.validateAdMonth(adMonth), validationFunctions.validateAdDate(adDate); var bsDate = calendarFunctions.getBsDateByAdDate(adYear, adMonth, adDate); return bsDate.bsMonth; }, bsDateFormat: function (dateFormatPattern, bsYear, bsMonth, bsDate) { validationFunctions.validateRequiredParameters({ dateFormatPattern: dateFormatPattern, bsYear: bsYear, bsMonth: bsMonth, bsDate: bsDate, }), validationFunctions.validateBsYear(bsYear), validationFunctions.validateBsMonth(bsMonth), validationFunctions.validateBsDate(bsDate); var eqAdDate = calendarFunctions.getAdDateByBsDate( bsYear, bsMonth, bsDate ), weekDay = eqAdDate.getDay() + 1, formattedDate = dateFormatPattern; return ( (formattedDate = formattedDate.replace( /%d/g, calendarFunctions.getNepaliNumber(bsDate) )), (formattedDate = formattedDate.replace( /%y/g, calendarFunctions.getNepaliNumber(bsYear) )), (formattedDate = formattedDate.replace( /%m/g, calendarFunctions.getNepaliNumber(bsMonth) )), (formattedDate = formattedDate.replace( /%M/g, calendarData.bsMonths[bsMonth - 1] )), (formattedDate = formattedDate.replace( /%D/g, calendarData.bsDays[weekDay - 1] )) ); }, parseFormattedBsDate: function (dateFormat, dateFormattedText) { validationFunctions.validateRequiredParameters({ dateFormat: dateFormat, dateFormattedText: dateFormattedText, }); for ( var diffTextNum = 0, extractedFormattedBsDate = { bsYear: null, bsMonth: null, bsDate: null, bsDay: null, }, i = 0; i < dateFormat.length; i++ ) if ("%" === dateFormat.charAt(i)) { var valueOf = dateFormat.substring(i, i + 2), endChar = dateFormat.charAt(i + 2), tempText = dateFormattedText.substring(i + diffTextNum), endIndex = "" !== endChar ? tempText.indexOf(endChar) : tempText.length, value = tempText.substring(0, endIndex); "%y" === valueOf ? ((extractedFormattedBsDate.bsYear = calendarFunctions.getNumberByNepaliNumber(value)), (diffTextNum += value.length - 2)) : "%d" === valueOf ? ((extractedFormattedBsDate.bsDate = calendarFunctions.getNumberByNepaliNumber(value)), (diffTextNum += value.length - 2)) : "%D" === valueOf ? ((extractedFormattedBsDate.bsDay = calendarData.bsDays.indexOf(value) + 1), (diffTextNum += value.length - 2)) : "%m" === valueOf ? ((extractedFormattedBsDate.bsMonth = calendarFunctions.getNumberByNepaliNumber(value)), (diffTextNum += value.length - 2)) : "%M" === valueOf && ((extractedFormattedBsDate.bsMonth = calendarData.bsMonths.indexOf(value) + 1), (diffTextNum += value.length - 2)); } if (!extractedFormattedBsDate.bsDay) { var eqAdDate = calendarFunctions.getAdDateByBsDate( extractedFormattedBsDate.bsYear, extractedFormattedBsDate.bsMonth, extractedFormattedBsDate.bsDate ); extractedFormattedBsDate.bsDay = eqAdDate.getDay() + 1; } return extractedFormattedBsDate; }, }), ($.fn.nepaliDatePicker = function (options) { var datePickerPlugin = { options: $.extend( { dateFormat: "%D, %M %d, %y", closeOnDateSelect: !0, defaultDate: "", minDate: null, maxDate: null, yearStart: calendarData.minBsYear, yearEnd: calendarData.maxBsYear, }, options ), init: function ($element) { $element.prop("readonly_removed", !0); var $nepaliDatePicker = $('