UNPKG

persian-date

Version:

Javascript date library for parsing, validating, manipulating, and formatting persian dates System.

1,579 lines (1,409 loc) 111 kB
/*! * * persian-date - 1.1.0 * Reza Babakhani <babakhani.reza@gmail.com> * http://babakhani.github.io/PersianWebToolkit/docs/persian-date/ * Under MIT license * * */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["persianDate"] = factory(); else root["persianDate"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 8); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var durationUnit = __webpack_require__(4).durationUnit; var Helpers = function () { function Helpers() { _classCallCheck(this, Helpers); } _createClass(Helpers, [{ key: 'toPersianDigit', /** * @description return converted string to persian digit * @param digit * @returns {string|*} */ value: function toPersianDigit(digit) { var latinDigit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return digit.toString().replace(/\d+/g, function (digit) { var enDigitArr = [], peDigitArr = [], i = void 0, j = void 0; for (i = 0; i < digit.length; i += 1) { enDigitArr.push(digit.charCodeAt(i)); } for (j = 0; j < enDigitArr.length; j += 1) { peDigitArr.push(String.fromCharCode(enDigitArr[j] + (!!latinDigit && latinDigit === true ? 1584 : 1728))); } return peDigitArr.join(''); }); } /** * @param number * @param targetLength * @returns {string} */ }, { key: 'leftZeroFill', value: function leftZeroFill(number, targetLength) { var output = number + ''; while (output.length < targetLength) { output = '0' + output; } return output; } /** * @description normalize duration params and return valid param * @return {{unit: *, value: *}} */ }, { key: 'normalizeDuration', value: function normalizeDuration() { var unit = void 0, value = void 0; if (typeof arguments[0] === 'string') { unit = arguments[0]; value = arguments[1]; } else { value = arguments[0]; unit = arguments[1]; } if (durationUnit.year.indexOf(unit) > -1) { unit = 'year'; } else if (durationUnit.month.indexOf(unit) > -1) { unit = 'month'; } else if (durationUnit.week.indexOf(unit) > -1) { unit = 'week'; } else if (durationUnit.day.indexOf(unit) > -1) { unit = 'day'; } else if (durationUnit.hour.indexOf(unit) > -1) { unit = 'hour'; } else if (durationUnit.minute.indexOf(unit) > -1) { unit = 'minute'; } else if (durationUnit.second.indexOf(unit) > -1) { unit = 'second'; } else if (durationUnit.millisecond.indexOf(unit) > -1) { unit = 'millisecond'; } return { unit: unit, value: value }; } /** * * @param number * @returns {number} */ }, { key: 'absRound', value: function absRound(number) { if (number < 0) { return Math.ceil(number); } else { return Math.floor(number); } } /** * * @param number * @return {number} */ }, { key: 'absFloor', value: function absFloor(number) { if (number < 0) { // -0 -> 0 return Math.ceil(number) || 0; } else { return Math.floor(number); } } }]); return Helpers; }(); module.exports = Helpers; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var TypeChecking = __webpack_require__(10); var Algorithms = __webpack_require__(2); var Helpers = __webpack_require__(0); var Duration = __webpack_require__(5); var Validator = __webpack_require__(11); var toPersianDigit = new Helpers().toPersianDigit; var leftZeroFill = new Helpers().leftZeroFill; var normalizeDuration = new Helpers().normalizeDuration; var fa = __webpack_require__(7); var en = __webpack_require__(6); /** * @description persian date class */ var PersianDateClass = function () { /** * @param input * @return {PersianDateClass} */ function PersianDateClass(input) { _classCallCheck(this, PersianDateClass); this.calendarType = PersianDateClass.calendarType; this.localType = PersianDateClass.localType; this.leapYearMode = PersianDateClass.leapYearMode; this.algorithms = new Algorithms(this); this.version = "1.1.0"; this._utcMode = false; if (this.localType !== 'fa') { this.formatPersian = false; } else { this.formatPersian = '_default'; } this.State = this.algorithms.State; this.setup(input); if (this.State.isInvalidDate) { // Return Date like message return new Date([-1, -1]); } return this; } /** * @param input */ _createClass(PersianDateClass, [{ key: 'setup', value: function setup(input) { // Convert Any thing to Gregorian Date if (TypeChecking.isDate(input)) { this._gDateToCalculators(input); } else if (TypeChecking.isArray(input)) { if (!Validator.validateInputArray(input)) { this.State.isInvalidDate = true; return false; } this.algorithmsCalc([input[0], input[1] ? input[1] : 1, input[2] ? input[2] : 1, input[3] ? input[3] : 0, input[4] ? input[4] : 0, input[5] ? input[5] : 0, input[6] ? input[6] : 0]); } else if (TypeChecking.isNumber(input)) { var fromUnix = new Date(input); this._gDateToCalculators(fromUnix); } // instance of pDate else if (input instanceof PersianDateClass) { this.algorithmsCalc([input.year(), input.month(), input.date(), input.hour(), input.minute(), input.second(), input.millisecond()]); } // ASP.NET JSON Date else if (input && input.substring(0, 6) === '/Date(') { var fromDotNet = new Date(parseInt(input.substr(6))); this._gDateToCalculators(fromDotNet); } else { var now = new Date(); this._gDateToCalculators(now); } } /** * @param input * @return {*} * @private */ }, { key: '_getSyncedClass', value: function _getSyncedClass(input) { var syncedCelander = PersianDateClass.toCalendar(this.calendarType).toLocale(this.localType).toLeapYearMode(this.leapYearMode); return new syncedCelander(input); } /** * @param inputgDate * @private */ }, { key: '_gDateToCalculators', value: function _gDateToCalculators(inputgDate) { this.algorithms.calcGregorian([inputgDate.getFullYear(), inputgDate.getMonth(), inputgDate.getDate(), inputgDate.getHours(), inputgDate.getMinutes(), inputgDate.getSeconds(), inputgDate.getMilliseconds()]); } /** * @since 1.0.0 * @description Helper method that return date range name like week days name, month names, month days names (specially in persian calendar). * @static * @return {*} */ }, { key: 'rangeName', /** * @since 1.0.0 * @description Helper method that return date range name like week days name, month names, month days names (specially in persian calendar). * @return {*} */ value: function rangeName() { var t = this.calendarType; if (this.localType === 'fa') { if (t === 'persian') { return fa.persian; } else { return fa.gregorian; } } else { if (t === 'persian') { return en.persian; } else { return en.gregorian; } } } /** * @since 1.0.0 * @param input * @return {PersianDateClass} */ }, { key: 'toLeapYearMode', value: function toLeapYearMode(input) { this.leapYearMode = input; if (input === 'astronomical' && this.calendarType == 'persian') { this.leapYearMode = 'astronomical'; } else if (input === 'algorithmic' && this.calendarType == 'persian') { this.leapYearMode = 'algorithmic'; } this.algorithms.updateFromGregorian(); return this; } /** * @since 1.0.0 * @static * @param input * @return {PersianDateClass} */ }, { key: 'toCalendar', /** * @since 1.0.0 * @param input * @return {PersianDateClass} */ value: function toCalendar(input) { this.calendarType = input; this.algorithms.updateFromGregorian(); return this; } /** * @since 1.0.0 * @static * @param input * @return {PersianDateClass} */ }, { key: 'toLocale', /** * @since 1.0.0 * @param input * @return {PersianDateClass} */ value: function toLocale(input) { this.localType = input; if (this.localType !== 'fa') { this.formatPersian = false; } else { this.formatPersian = '_default'; } return this; } /** * @return {*} * @private */ }, { key: '_locale', value: function _locale() { var t = this.calendarType; if (this.localType === 'fa') { if (t === 'persian') { return fa.persian; } else { return fa.gregorian; } } else { if (t === 'persian') { return en.persian; } else { return en.gregorian; } } } /** * @param input * @private */ }, { key: '_weekName', value: function _weekName(input) { return this._locale().weekdays[input - 1]; } /** * @param input * @private */ }, { key: '_weekNameShort', value: function _weekNameShort(input) { return this._locale().weekdaysShort[input - 1]; } /** * @param input * @private */ }, { key: '_weekNameMin', value: function _weekNameMin(input) { return this._locale().weekdaysMin[input - 1]; } /** * @param input * @return {*} * @private */ }, { key: '_dayName', value: function _dayName(input) { return this._locale().persianDaysName[input - 1]; } /** * @param input * @private */ }, { key: '_monthName', value: function _monthName(input) { return this._locale().months[input - 1]; } /** * @param input * @private */ }, { key: '_monthNameShort', value: function _monthNameShort(input) { return this._locale().monthsShort[input - 1]; } /** * @param obj * @returns {boolean} */ }, { key: 'isPersianDate', /** * @param obj * @return {boolean} */ value: function isPersianDate(obj) { return obj instanceof PersianDateClass; } /** * @returns {PersianDate} */ }, { key: 'clone', value: function clone() { return this._getSyncedClass(this.State.gDate); } /** * @since 1.0.0 * @param dateArray * @return {*} */ }, { key: 'algorithmsCalc', value: function algorithmsCalc(dateArray) { if (this.isPersianDate(dateArray)) { dateArray = [dateArray.year(), dateArray.month(), dateArray.date(), dateArray.hour(), dateArray.minute(), dateArray.second(), dateArray.millisecond()]; } if (this.calendarType === 'persian' && this.leapYearMode == 'algorithmic') { return this.algorithms.calcPersian(dateArray); } else if (this.calendarType === 'persian' && this.leapYearMode == 'astronomical') { return this.algorithms.calcPersiana(dateArray); } else if (this.calendarType === 'gregorian') { dateArray[1] = dateArray[1] - 1; return this.algorithms.calcGregorian(dateArray); } } /** * @since 1.0.0 * @return {*} */ }, { key: 'calendar', value: function calendar() { var key = void 0; if (this.calendarType == 'persian') { if (this.leapYearMode == 'astronomical') { key = 'persianAstro'; } else if (this.leapYearMode == 'algorithmic') { key = 'persianAlgo'; } } else { key = 'gregorian'; } return this.State[key]; } /** * @description return Duration object * @param input * @param key * @returns {Duration} */ }, { key: 'duration', /** * @description return Duration object * @param input * @param key * @returns {Duration} */ value: function duration(input, key) { return new Duration(input, key); } /** * @description check if passed object is duration * @param obj * @returns {boolean} */ }, { key: 'isDuration', /** * @description check if passed object is duration * @param obj * @returns {boolean} */ value: function isDuration(obj) { return obj instanceof Duration; } /** * @param input * @returns {*} */ }, { key: 'years', value: function years(input) { return this.year(input); } /** * @param input * @returns {*} */ }, { key: 'year', value: function year(input) { if (input || input === 0) { this.algorithmsCalc([input, this.month(), this.date(), this.hour(), this.minute(), this.second(), this.millisecond()]); return this; } else { return this.calendar().year; } } /** * @param input * @returns {*} */ }, { key: 'month', value: function month(input) { if (input || input === 0) { this.algorithmsCalc([this.year(), input, this.date()]); return this; } else { return this.calendar().month + 1; } } /** * Day of week * @returns {Function|Date.toJSON.day|date_json.day|PersianDate.day|day|output.day|*} */ }, { key: 'days', value: function days() { return this.day(); } /** * @returns {Function|Date.toJSON.day|date_json.day|PersianDate.day|day|output.day|*} */ }, { key: 'day', value: function day() { return this.calendar().weekday; } /** * Day of Months * @param input * @returns {*} */ }, { key: 'dates', value: function dates(input) { return this.date(input); } /** * @param input * @returns {*} */ }, { key: 'date', value: function date(input) { if (input || input === 0) { this.algorithmsCalc([this.year(), this.month(), input]); return this; } else { return this.calendar().day; } } /** * @param input * @returns {*} */ }, { key: 'hour', value: function hour(input) { return this.hours(input); } /** * @param input * @returns {*} */ }, { key: 'hours', value: function hours(input) { if (input || input === 0) { if (input === 0) { input = 24; } this.algorithmsCalc([this.year(), this.month(), this.date(), input]); return this; } else { return this.State.gDate.getHours(); } } /** * @param input * @returns {*} */ }, { key: 'minute', value: function minute(input) { return this.minutes(input); } /** * @param input * @returns {*} */ }, { key: 'minutes', value: function minutes(input) { if (input || input === 0) { this.algorithmsCalc([this.year(), this.month(), this.date(), this.hour(), input]); return this; } else { return this.State.gDate.getMinutes(); } } /** * @param input * @returns {*} */ }, { key: 'second', value: function second(input) { return this.seconds(input); } /** * @param input * @returns {*} */ }, { key: 'seconds', value: function seconds(input) { if (input || input === 0) { this.algorithmsCalc([this.year(), this.month(), this.date(), this.hour(), this.minute(), input]); return this; } else { return this.State.gDate.getSeconds(); } } /** * @param input * @returns {*} * Getter Setter */ }, { key: 'millisecond', value: function millisecond(input) { return this.milliseconds(input); } /** * @param input * @returns {*} */ }, { key: 'milliseconds', value: function milliseconds(input) { if (input || input === 0) { this.algorithmsCalc([this.year(), this.month(), this.date(), this.hour(), this.minute(), this.second(), input]); return this; } else { return this.State.gregorian.millisecond; } } /** * Return Milliseconds since the Unix Epoch (1318874398806) * @returns {*} * @private */ // _valueOf () { // return this.State.gDate.valueOf(); // } }, { key: 'unix', /** * Return Unix Timestamp (1318874398) * @param timestamp * @returns {*} */ value: function unix(timestamp) { var output = void 0; if (timestamp) { return this._getSyncedClass(timestamp * 1000); } else { var str = this.State.gDate.valueOf().toString(); output = str.substring(0, str.length - 3); } return parseInt(output); } /** * @returns {*} */ }, { key: 'valueOf', value: function valueOf() { return this.State.gDate.valueOf(); } /** * @param year * @param month * @returns {*} * @since 1.0.0 */ }, { key: 'getFirstWeekDayOfMonth', /** * @param year * @param month * @returns {*} * @since 1.0.0 */ value: function getFirstWeekDayOfMonth(year, month) { return this._getSyncedClass([year, month, 1]).day(); } /** * @param input * @param val * @param asFloat * @returns {*} */ }, { key: 'diff', value: function diff(input, val, asFloat) { var self = this, inputMoment = input, zoneDiff = 0, diff = self.State.gDate - inputMoment.toDate() - zoneDiff, year = self.year() - inputMoment.year(), month = self.month() - inputMoment.month(), date = (self.date() - inputMoment.date()) * -1, output = void 0; if (val === 'months' || val === 'month') { output = year * 12 + month + date / 30; } else if (val === 'years' || val === 'year') { output = year + (month + date / 30) / 12; } else { output = val === 'seconds' || val === 'second' ? diff / 1e3 : // 1000 val === 'minutes' || val === 'minute' ? diff / 6e4 : // 1000 * 60 val === 'hours' || val === 'hour' ? diff / 36e5 : // 1000 * 60 * 60 val === 'days' || val === 'day' ? diff / 864e5 : // 1000 * 60 * 60 * 24 val === 'weeks' || val === 'week' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7 diff; } return asFloat ? output : Math.round(output); } /** * @param key * @returns {*} */ }, { key: 'startOf', value: function startOf(key) { var syncedCelander = PersianDateClass.toCalendar(this.calendarType).toLocale(this.localType); var newArray = new PersianDateClass(this.valueOf() - (this.calendar().weekday - 1) * 86400000).toArray(); // Simplify this\ /* jshint ignore:start */ switch (key) { case 'years': case 'year': return new syncedCelander([this.year(), 1, 1]); case 'months': case 'month': return new syncedCelander([this.year(), this.month(), 1]); case 'days': case 'day': return new syncedCelander([this.year(), this.month(), this.date(), 0, 0, 0]); case 'hours': case 'hour': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), 0, 0]); case 'minutes': case 'minute': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), this.minutes(), 0]); case 'seconds': case 'second': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), this.minutes(), this.seconds()]); case 'weeks': case 'week': return new syncedCelander(newArray); default: return this.clone(); } /* jshint ignore:end */ } /** * @param key * @returns {*} */ /* eslint-disable no-case-declarations */ }, { key: 'endOf', value: function endOf(key) { var syncedCelander = PersianDateClass.toCalendar(this.calendarType).toLocale(this.localType); // Simplify this switch (key) { case 'years': case 'year': var days = this.isLeapYear() ? 30 : 29; return new syncedCelander([this.year(), 12, days, 23, 59, 59]); case 'months': case 'month': var monthDays = this.daysInMonth(this.year(), this.month()); return new syncedCelander([this.year(), this.month(), monthDays, 23, 59, 59]); case 'days': case 'day': return new syncedCelander([this.year(), this.month(), this.date(), 23, 59, 59]); case 'hours': case 'hour': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), 59, 59]); case 'minutes': case 'minute': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), this.minutes(), 59]); case 'seconds': case 'second': return new syncedCelander([this.year(), this.month(), this.date(), this.hours(), this.minutes(), this.seconds()]); case 'weeks': case 'week': var weekDayNumber = this.calendar().weekday; return new syncedCelander([this.year(), this.month(), this.date() + (7 - weekDayNumber)]); default: return this.clone(); } /* eslint-enable no-case-declarations */ } /** * @returns {*} */ }, { key: 'sod', value: function sod() { return this.startOf('day'); } /** * @returns {*} */ }, { key: 'eod', value: function eod() { return this.endOf('day'); } /** Get the timezone offset in minutes. * @return {*} */ }, { key: 'zone', value: function zone(input) { if (input || input === 0) { this.State.zone = input; return this; } else { return this.State.zone; } } /** * @returns {PersianDate} */ }, { key: 'local', value: function local() { var utcStamp = void 0; if (this._utcMode) { var ThatDayOffset = new Date(this.toDate()).getTimezoneOffset(); var offsetMils = ThatDayOffset * 60 * 1000; if (ThatDayOffset < 0) { utcStamp = this.valueOf() - offsetMils; } else { /* istanbul ignore next */ utcStamp = this.valueOf() + offsetMils; } this.toCalendar(PersianDateClass.calendarType); var utcDate = new Date(utcStamp); this._gDateToCalculators(utcDate); this._utcMode = false; this.zone(ThatDayOffset); return this; } else { return this; } } /** * @param input * @return {*} */ }, { key: 'utc', /** * @description Current date/time in UTC mode * @param input * @returns {*} */ value: function utc(input) { var utcStamp = void 0; if (input) { return this._getSyncedClass(input).utc(); } if (this._utcMode) { return this; } else { var offsetMils = this.zone() * 60 * 1000; if (this.zone() < 0) { utcStamp = this.valueOf() + offsetMils; } else { /* istanbul ignore next */ utcStamp = this.valueOf() - offsetMils; } var utcDate = new Date(utcStamp), d = this._getSyncedClass(utcDate); this.algorithmsCalc(d); this._utcMode = true; this.zone(0); return this; } } /** * @returns {boolean} */ }, { key: 'isUtc', value: function isUtc() { return this._utcMode; } /** * @returns {boolean} * @link https://fa.wikipedia.org/wiki/%D8%B3%D8%A7%D8%B9%D8%AA_%D8%AA%D8%A7%D8%A8%D8%B3%D8%AA%D8%A7%D9%86%DB%8C */ }, { key: 'isDST', value: function isDST() { var month = this.month(), day = this.date(); if (month == 1 && day > 1 || month == 6 && day < 31 || month < 6 && month >= 2) { return true; } else { return false; } } /** * @returns {boolean} */ }, { key: 'isLeapYear', value: function isLeapYear(year) { if (year === undefined) { year = this.year(); } if (this.calendarType == 'persian' && this.leapYearMode === 'algorithmic') { return this.algorithms.leap_persian(year); } if (this.calendarType == 'persian' && this.leapYearMode === 'astronomical') { return this.algorithms.leap_persiana(year); } else if (this.calendarType == 'gregorian') { return this.algorithms.leap_gregorian(year); } } /** * @param yearInput * @param monthInput * @returns {number} */ }, { key: 'daysInMonth', value: function daysInMonth(yearInput, monthInput) { var year = yearInput ? yearInput : this.year(), month = monthInput ? monthInput : this.month(); if (this.calendarType === 'persian') { if (month < 1 || month > 12) return 0; if (month < 7) return 31; if (month < 12) return 30; if (this.isLeapYear(year)) { return 30; } return 29; } if (this.calendarType === 'gregorian') { return new Date(year, month, 0).getDate(); } } /** * @description Return Native Javascript Date * @returns {*|PersianDate.gDate} */ }, { key: 'toDate', value: function toDate() { return this.State.gDate; } /** * @description Returns Array Of Persian Date * @returns {array} */ }, { key: 'toArray', value: function toArray() { return [this.year(), this.month(), this.date(), this.hour(), this.minute(), this.second(), this.millisecond()]; } /** * @returns {*} */ }, { key: 'formatNumber', value: function formatNumber() { var output = void 0, self = this; // if default conf dosent set follow golbal config if (this.formatPersian === '_default') { if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { /* istanbul ignore next */ if (self.formatPersian === false) { output = false; } else { // Default Conf output = true; } } /* istanbul ignore next */ else { if (window.formatPersian === false) { output = false; } else { // Default Conf output = true; } } } else { if (this.formatPersian === true) { output = true; } else if (this.formatPersian === false) { output = false; } else { Error('Invalid Config "formatPersian" !!'); } } return output; } /** * @param inputString * @returns {*} */ }, { key: 'format', value: function format(inputString) { if (this.State.isInvalidDate) { return false; } var self = this, formattingTokens = /([[^[]*])|(\\)?(Mo|MM?M?M?|Do|DD?D?D?|dddddd?|ddddd?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|zz?|ZZ?|X|LT|ll?l?l?|LL?L?L?)/g, info = { year: self.year(), month: self.month(), hour: self.hours(), minute: self.minutes(), second: self.seconds(), date: self.date(), timezone: self.zone(), unix: self.unix() }, formatToPersian = self.formatNumber(); var checkPersian = function checkPersian(i) { if (formatToPersian) { return toPersianDigit(i); } else { return i; } }; /* jshint ignore:start */ function replaceFunction(input) { switch (input) { // AM/PM case 'a': { if (formatToPersian) return info.hour >= 12 ? 'ب ظ' : 'ق ظ';else return info.hour >= 12 ? 'PM' : 'AM'; } // Hours (Int) case 'H': { return checkPersian(info.hour); } case 'HH': { return checkPersian(leftZeroFill(info.hour, 2)); } case 'h': { return checkPersian(info.hour % 12); } case 'hh': { return checkPersian(leftZeroFill(info.hour % 12, 2)); } // Minutes case 'm': { return checkPersian(leftZeroFill(info.minute, 2)); } // Two Digit Minutes case 'mm': { return checkPersian(leftZeroFill(info.minute, 2)); } // Second case 's': { return checkPersian(info.second); } case 'ss': { return checkPersian(leftZeroFill(info.second, 2)); } // Day (Int) case 'D': { return checkPersian(leftZeroFill(info.date)); } // Return Two Digit case 'DD': { return checkPersian(leftZeroFill(info.date, 2)); } // Return day Of Month case 'DDD': { var t = self.startOf('year'); return checkPersian(leftZeroFill(self.diff(t, 'days'), 3)); } // Return Day of Year case 'DDDD': { var _t = self.startOf('year'); return checkPersian(leftZeroFill(self.diff(_t, 'days'), 3)); } // Return day Of week case 'd': { return checkPersian(self.calendar().weekday); } // Return week day name abbr case 'ddd': { return self._weekNameShort(self.calendar().weekday); } case 'dddd': { return self._weekName(self.calendar().weekday); } // Return Persian Day Name case 'ddddd': { return self._dayName(self.calendar().day); } // Return Persian Day Name case 'dddddd': { return self._weekNameMin(self.calendar().weekday); } // Return Persian Day Name case 'w': { var _t2 = self.startOf('year'), day = parseInt(self.diff(_t2, 'days') / 7) + 1; return checkPersian(day); } // Return Persian Day Name case 'ww': { var _t3 = self.startOf('year'), _day = leftZeroFill(parseInt(self.diff(_t3, 'days') / 7) + 1, 2); return checkPersian(_day); } // Month (Int) case 'M': { return checkPersian(info.month); } // Two Digit Month (Str) case 'MM': { return checkPersian(leftZeroFill(info.month, 2)); } // Abbr String of Month (Str) case 'MMM': { return self._monthNameShort(info.month); } // Full String name of Month (Str) case 'MMMM': { return self._monthName(info.month); } // Year // Two Digit Year (Str) case 'YY': { var yearDigitArray = info.year.toString().split(''); return checkPersian(yearDigitArray[2] + yearDigitArray[3]); } // Full Year (Int) case 'YYYY': { return checkPersian(info.year); } /* istanbul ignore next */ case 'Z': { var flag = '+', hours = Math.round(info.timezone / 60), minutes = info.timezone % 60; if (minutes < 0) { minutes *= -1; } if (hours < 0) { flag = '-'; hours *= -1; } var z = flag + leftZeroFill(hours, 2) + ':' + leftZeroFill(minutes, 2); return checkPersian(z); } /* istanbul ignore next */ case 'ZZ': { var _flag = '+', _hours = Math.round(info.timezone / 60), _minutes = info.timezone % 60; if (_minutes < 0) { _minutes *= -1; } if (_hours < 0) { _flag = '-'; _hours *= -1; } var _z = _flag + leftZeroFill(_hours, 2) + '' + leftZeroFill(_minutes, 2); return checkPersian(_z); } /* istanbul ignore next */ case 'X': { return self.unix(); } // 8:30 PM case 'LT': { return self.format('H:m a'); } // 09/04/1986 case 'L': { return self.format('YYYY/MM/DD'); } // 9/4/1986 case 'l': { return self.format('YYYY/M/D'); } // September 4th 1986 case 'LL': { return self.format('MMMM DD YYYY'); } // Sep 4 1986 case 'll': { return self.format('MMM DD YYYY'); } //September 4th 1986 8:30 PM case 'LLL': { return self.format('MMMM YYYY DD H:m a'); } // Sep 4 1986 8:30 PM case 'lll': { return self.format('MMM YYYY DD H:m a'); } //Thursday, September 4th 1986 8:30 PM case 'LLLL': { return self.format('dddd D MMMM YYYY H:m a'); } // Thu, Sep 4 1986 8:30 PM case 'llll': { return self.format('ddd D MMM YYYY H:m a'); } } } /* jshint ignore:end */ if (inputString) { return inputString.replace(formattingTokens, replaceFunction); } else { var _inputString = 'YYYY-MM-DD HH:mm:ss a'; return _inputString.replace(formattingTokens, replaceFunction); } } /** * @param key * @param value * @returns {PersianDate} */ }, { key: 'add', value: function add(key, value) { if (value === 0) { return this; } var unit = normalizeDuration(key, value).unit, arr = this.toArray(); value = normalizeDuration(key, value).value; if (unit === 'year') { var normalizedDate = arr[2], monthDays = this.daysInMonth(arr[0] + value, arr[1]); if (arr[2] > monthDays) { normalizedDate = monthDays; } var tempDate = new PersianDateClass([arr[0] + value, arr[1], normalizedDate, arr[3], arr[4], arr[5], arr[6], arr[7]]); return tempDate; } if (unit === 'month') { var tempYear = Math.floor(value / 12); var remainingMonth = value - tempYear * 12, calcedMonth = null; if (arr[1] + remainingMonth > 12) { tempYear += 1; calcedMonth = arr[1] + remainingMonth - 12; } else { calcedMonth = arr[1] + remainingMonth; } var normalizaedDate = arr[2], tempDateArray = new PersianDateClass([arr[0] + tempYear, calcedMonth, 1, arr[3], arr[4], arr[5], arr[6], arr[7]]).toArray(), _monthDays = this.daysInMonth(arr[0] + tempYear, calcedMonth); if (arr[2] > _monthDays) { normalizaedDate = _monthDays; } return new PersianDateClass([tempDateArray[0], tempDateArray[1], normalizaedDate, tempDateArray[3], tempDateArray[4], tempDateArray[5], tempDateArray[6], tempDateArray[7]]); } if (unit === 'day') { var calcedDay = new PersianDateClass(this.valueOf()).hour(12), newMillisecond = calcedDay.valueOf() + value * 86400000, newDate = new PersianDateClass(newMillisecond); return newDate.hour(arr[3]); } if (unit === 'week') { var _calcedDay = new PersianDateClass(this.valueOf()).hour(12), _n