Created
July 9, 2020 20:48
-
-
Save pradipchitrakar/2023ca88848cd21c78d25a8c8f2f624d to your computer and use it in GitHub Desktop.
Converts date in Bikram Sambat to A.D. and vice versa in typescript/javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class NepaliDateConverterService { | |
| nums = { | |
| 0: '०', | |
| 1: '१', | |
| 2: '२', | |
| 3: '३', | |
| 4: '४', | |
| 5: '५', | |
| 6: '६', | |
| 7: '७', | |
| 8: '८', | |
| 9: '९' | |
| }; | |
| defaults = { | |
| lang: 'ne', // possible values: ne for nepali text, en for english text | |
| // dateFormat: 'yyyy/mm/dd', // not implemented yet | |
| monthFormat: 'full', // possible values: full for full name, short for short name | |
| daysFormat: 'min', // possible values: full for full name, short for short name and min for minified name | |
| }; | |
| ne = { | |
| monthsName: ['बैशाख', 'जेष्ठ', 'आषाढ', 'श्रावण', 'भाद्र', 'आश्विन', 'कार्तिक', 'मंसिर', 'पौष', 'माघ', 'फाल्गुन', 'चैत्र'], | |
| monthsShortName: ['बै', 'जे', 'आषा', 'श्रा', 'भा', 'आश', 'का', 'मं', 'पौ', 'मा', 'फा', 'चै'], | |
| }; | |
| en = { | |
| monthsName: ['Baisakh', 'Jestha', 'Ashadh', 'Shrawan', 'Bhadra', 'Ashwin', 'Kartik', 'Mangsir', 'Paush', 'Mangh', 'Falgun', 'Chaitra'], | |
| monthsShortName: ['Bai', 'Je', 'As', 'Shra', 'Bha', 'Ash', 'Kar', 'Mang', 'Pau', 'Ma', 'Fal', 'Chai'], | |
| }; | |
| engDaysName = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | |
| engDaysShortName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | |
| engMonthsName = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; | |
| engMonthsShortName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | |
| daysInYear = 365; | |
| minMonth = 1; | |
| minDays = 1; | |
| maxMonth = 12; | |
| maxDays = 32; | |
| // tslint:disable-next-line:variable-name | |
| base_ad = {year: 2017, month: 2, day: 11, dayOfWeek: 6}; // dayOfWeek: 0 for sunday, 1 for monday and so on | |
| // tslint:disable-next-line:variable-name | |
| base_bs = {year: 2073, month: 10, day: 29, dayOfWeek: 6}; | |
| // tslint:disable-next-line:variable-name | |
| calendar_data = { | |
| '1978': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1979': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1980': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '1981': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '1982': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1983': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1984': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '1985': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '1986': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1987': [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1988': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '1989': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1990': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1991': [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1992': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '1993': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1994': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1995': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '1996': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '1997': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1998': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '1999': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2000': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365 ], | |
| '2001': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2002': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2003': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2004': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365 ], | |
| '2005': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2006': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2007': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2008': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365 ], | |
| '2009': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2010': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2011': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2012': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '2013': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2014': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2015': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2016': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '2017': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2018': [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2019': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '2020': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2021': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2022': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '2023': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '2024': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2025': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2026': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2027': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365 ], | |
| '2028': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2029': [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2030': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2031': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365 ], | |
| '2032': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2033': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2034': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2035': [ 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365 ], | |
| '2036': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2037': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2038': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2039': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '2040': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2041': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2042': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2043': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '2044': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2045': [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2046': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2047': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2048': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2049': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '2050': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '2051': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2052': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2053': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '2054': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '2055': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2056': [ 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2057': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2058': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 365 ], | |
| '2059': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2060': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2061': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2062': [ 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31, 365 ], | |
| '2063': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2064': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2065': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2066': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31, 365 ], | |
| '2067': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2068': [ 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2069': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2070': [ 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30, 365 ], | |
| '2071': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2072': [ 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2073': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31, 366 ], | |
| '2074': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2075': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2076': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '2077': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31, 366 ], | |
| '2078': [ 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2079': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30, 365 ], | |
| '2080': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30, 365 ], | |
| '2081': [ 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2082': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2083': [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2084': [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2085': [ 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2086': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2087': [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2088': [ 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2089': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2090': [ 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2091': [ 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2092': [ 31, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366 ] | |
| }; | |
| /* | |
| * gathered data below; if anybody can validate below, thanks! | |
| * A hacky way is to iterate for the unknown dates is to use daysPerYear and loop through | |
| * | |
| '2093': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2094': [ 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30, 365 ], | |
| '2095': [ 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30, 366 ], | |
| '2096': [ 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30, 364 ], | |
| '2097': [ 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30, 366 ], | |
| '2098': [ 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 30, 31, 366 ], | |
| '2099': [ 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30, 365 ], | |
| '2100': [ 31, 32, 31, 32, 30, 31, 30, 29, 30, 29, 30, 30, 365 ] | |
| */ | |
| getNepaliNumber(strNum) { | |
| const arrNumNe = strNum.toString().split('').map( (ch) => { | |
| if (ch === '.' || ch === ',') { | |
| return ch; | |
| } | |
| return this.nums[Number(ch)]; | |
| }); | |
| return arrNumNe.join(''); | |
| } | |
| getNepDayOfWeek(inp, opts?) { | |
| if (Object.prototype.toString.call(inp) === '[object Object]') { | |
| opts = inp; | |
| inp = undefined; | |
| } | |
| if (inp !== undefined && Object.prototype.toString.call(inp) !== '[object Date]' && typeof inp !== 'number') { | |
| return new TypeError('Expected a date object or a number'); | |
| } | |
| if (typeof inp === 'number' && (inp < 0 || inp > 6)) { | |
| return new RangeError('Expected the value of inp between 0-6'); | |
| } | |
| opts = opts || {}; | |
| const data: any = {}; | |
| data.ne = { | |
| full: ['आइतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहिबार', 'शुक्रबार', 'शनिबार'], | |
| short: ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि'], | |
| min: ['आ', 'सो', 'मं', 'बु', 'बि', 'शु', 'श'] | |
| }; | |
| data.en = { | |
| full: ['Aaitabaar', 'Sombaar', 'Mangalbaar', 'Budhabaar', 'Bihibaar', 'Shukrabaar', 'Shanibaar'], | |
| short: ['Aaita', 'Som', 'Mangal', 'Budha', 'Bihi', 'Shukra', 'Shani'], | |
| min: ['Aai', 'So', 'Man', 'Bu', 'Bi', 'Shu', 'Sha'] | |
| }; | |
| let lang = 'ne'; | |
| if (opts.lang === 'en') { | |
| lang = 'en'; | |
| } | |
| if (inp === undefined) { | |
| inp = new Date().getDay(); | |
| } | |
| if (Object.prototype.toString.call(inp) === '[object Date]') { | |
| inp = inp.getDay(); | |
| } | |
| if (!opts.type) { | |
| const nepday = { | |
| full: data[lang].full[inp], | |
| short: data[lang].short[inp], | |
| min: data[lang].min[inp] | |
| }; | |
| return nepday; | |
| } | |
| switch (opts.type) { | |
| case 'short': | |
| return data[lang].short[inp]; | |
| case 'min': | |
| return data[lang].min[inp]; | |
| default: | |
| return data[lang].full[inp]; | |
| } | |
| } | |
| constructor() { | |
| } | |
| countDaysInYear(year) { | |
| if (typeof this.calendar_data[year] === 'undefined') { | |
| return this.daysInYear; | |
| } | |
| return this.calendar_data[year][12]; | |
| } | |
| isLeapYear(year) { | |
| return (this.daysInYear !== this.countDaysInYear(year)); | |
| } | |
| countBSDays(date) { | |
| let dayCount = 0; | |
| const dateArr = date.split('/').map((str) => { | |
| return Number(str); | |
| }); | |
| const dateObj = {year: dateArr[0], month: dateArr[1], day: dateArr[2]}; | |
| let inc = false; | |
| if (dateArr[0] > this.base_bs.year) { | |
| inc = true; | |
| } else if (dateArr[0] === this.base_bs.year && dateArr[1] > this.base_bs.month) { | |
| inc = true; | |
| } else if (dateArr[0] === this.base_bs.year && dateArr[1] === this.base_bs.month && dateArr[2] > this.base_bs.day) { | |
| inc = true; | |
| } | |
| let start: any = {}; | |
| let end: any = {}; | |
| let factor = 1; | |
| if (inc === true) { | |
| start = this.base_bs; | |
| end = dateObj; | |
| } else { | |
| start = dateObj; | |
| end = this.base_bs; | |
| factor = -1; | |
| } | |
| for (let i = start.year; i <= end.year; i++) { | |
| dayCount += this.calendar_data[i][12]; | |
| } | |
| for (let i = 0; i < start.month - 1; i++) { | |
| dayCount -= this.calendar_data[start.year][i]; | |
| } | |
| // dayCount += calendar_data[start.year][12 - 1]; | |
| for (let i = end.month - 1; i < 12; i++) { | |
| dayCount -= this.calendar_data[end.year][i]; | |
| } | |
| dayCount -= start.day; | |
| dayCount += end.day; | |
| return (dayCount * factor); | |
| } | |
| countADDays(date) { | |
| const dayCount = 0, i = 0; | |
| const dateArr = date.split('/').map((str) => { | |
| return Number(str); | |
| }); | |
| const dateObj = {year: dateArr[0], month: dateArr[1] - 1, day: dateArr[2]}; | |
| const date1 = new Date(this.base_ad.year, this.base_ad.month - 1, this.base_ad.day); | |
| const date2 = new Date(dateObj.year, dateObj.month, dateObj.day); | |
| const timeDiff = date2.getTime() - date1.getTime(); | |
| const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); | |
| return {diffDays, dateInAd: date2}; | |
| } | |
| offsetBSDays(dayData) { | |
| let dayCount = dayData.diffDays; | |
| const dateInAd = dayData.dateInAd; | |
| const bs_date = JSON.parse(JSON.stringify(this.base_bs)); | |
| if (dayCount >= 0) { | |
| bs_date.day += dayCount; | |
| while (bs_date.day > this.calendar_data[bs_date.year][bs_date.month - 1]) { | |
| bs_date.day -= this.calendar_data[bs_date.year][bs_date.month - 1]; | |
| bs_date.month++; | |
| if (bs_date.month > 12) { | |
| bs_date.year++; | |
| bs_date.month = 1; | |
| } | |
| } | |
| } else { | |
| dayCount = Math.abs(dayCount); | |
| while (dayCount >= 0) { | |
| if (dayCount < this.calendar_data[bs_date.year][bs_date.month - 1]) { | |
| dayCount = this.calendar_data[bs_date.year][bs_date.month - 1] - dayCount; | |
| break; | |
| } | |
| dayCount -= this.calendar_data[bs_date.year][bs_date.month - 1]; | |
| bs_date.month--; | |
| if (bs_date.month === 0) { | |
| bs_date.year--; | |
| bs_date.month = 12; | |
| } | |
| } | |
| bs_date.day = dayCount; | |
| } | |
| const month = dateInAd.getMonth(), | |
| dayOfWeek = dateInAd.getDay(); | |
| const npDayOfWeek = this.getNepDayOfWeek(dayOfWeek), | |
| enDayOfWeek = this.getNepDayOfWeek(dayOfWeek, {lang: 'en'}); | |
| const totalDays = this.calendar_data[bs_date.year][bs_date.month - 1] | |
| const dateObj = { | |
| ne: { | |
| year: this.getNepaliNumber(bs_date.year), | |
| month: this.getNepaliNumber(bs_date.month), | |
| day: this.getNepaliNumber(bs_date.day), | |
| strMonth: this.ne.monthsName[bs_date.month - 1], | |
| strShortMonth: this.ne.monthsShortName[bs_date.month - 1], | |
| dayOfWeek: this.getNepaliNumber(dayOfWeek), | |
| strDayOfWeek: npDayOfWeek['full'], | |
| strShortDayOfWeek: npDayOfWeek['short'], | |
| strMinDayOfWeek: npDayOfWeek['min'], | |
| totalDaysInMonth: this.getNepaliNumber(totalDays) | |
| }, | |
| en: { | |
| year: bs_date.year, | |
| month: bs_date.month, | |
| day: bs_date.day, | |
| strMonth: this.en.monthsName[bs_date.month - 1], | |
| strShortMonth: this.en.monthsShortName[bs_date.month - 1], | |
| dayOfWeek, | |
| strDayOfWeek: enDayOfWeek['full'], | |
| strShortDayOfWeek: enDayOfWeek['short'], | |
| strMinDayOfWeek: enDayOfWeek['min'], | |
| totalDaysInMonth: totalDays | |
| } | |
| } | |
| return dateObj; | |
| } | |
| offsetADDays(dayCount) { | |
| const date = new Date(this.base_ad.year, this.base_ad.month - 1, this.base_ad.day); | |
| date.setDate(date.getDate() + dayCount); | |
| const month = date.getMonth(), | |
| dayOfWeek = date.getDay(); | |
| const dateObj = { | |
| year: date.getFullYear(), | |
| month: month + 1, | |
| strMonth: this.engMonthsName[month], | |
| strShortMonth: this.engMonthsShortName[month], | |
| day: date.getDate(), | |
| dayOfWeek, | |
| strDayOfWeek: this.engDaysName[dayOfWeek], | |
| strShortDayOfWeek: this.engDaysShortName[dayOfWeek] | |
| } | |
| return dateObj; | |
| } | |
| bs2ad(date) { | |
| return this.offsetADDays(this.countBSDays(date)); | |
| } | |
| ad2bs(date) { | |
| return this.offsetBSDays(this.countADDays(date)); | |
| } | |
| } | |
| // Example | |
| // const date = new NepaliDateConverterService(); | |
| // console.log(date.ad2bs('2020/7/9')); | |
| // console.log(date.bs2ad('2077/3/20')); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment