Skip to content

Commit 6cf9d14

Browse files
authored
feat: add method formatEnglishDate and formatEnglishDateInNepali (#94)
1 parent 29cda16 commit 6cf9d14

File tree

12 files changed

+1049
-318
lines changed

12 files changed

+1049
-318
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,17 @@ You can set individual components of a `NepaliDate` object using the following m
135135
#### Formatting the Nepali date
136136

137137
You can format a `NepaliDate` object as a string using the `format()` and `formatNepali()` methods.
138+
Additionally, you can convert the corresponding English date to a string using the `formatEnglishDate()` and `formatEnglishDateInNepali()` methods.
138139

139140
- `format(formatStr)`: Returns a string representation (in English) of the `NepaliDate` object in the specified format.
140-
- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari) script of the `NepaliDate` object in the specified format.
141+
- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the `NepaliDate` object in the specified format.
142+
- `formatEnglishDate(formatStr)`: Returns a string representation (in English) of the English Date in the specified format.
143+
- `formatEnglishDateInNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the English Date in the specified format.
141144

142145
```javascript
143146
const now = new NepaliDate(2079, 5, 3, 16, 14)
144147
console.log(now.format('YYYY-MM-DD hh:mm A')) // Outputs: 2079-06-03 04:14 PM
148+
console.log(now.formatEnglishDate('YYYY-MM-DD hh:mm A')) // Outputs: 2022-08-19 04:14 PM
145149
```
146150

147151
The date formatting will follow the format codes mentioned below, which are similar to the date formats used in day.js.

src/NepaliDate.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import dateConverter from './dateConverter'
2-
import { format, formatNepali, nepaliDateToString } from './format'
2+
import {
3+
format,
4+
formatEnglishDate,
5+
formatEnglishDateInNepali,
6+
formatNepali,
7+
nepaliDateToString,
8+
} from './format'
9+
310
import { parse, parseFormat } from './parse'
411
import { getDate, getNepalDateAndTime } from './utils'
512
import { validateTime } from './validators'
@@ -556,7 +563,7 @@ class NepaliDate {
556563
/**
557564
* Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format.
558565
* @param formatStr The format string for the desired output.
559-
* @returns {string} A string representation of the NepaliDate object in the specified format.
566+
* @returns {string} The formatted Date string in Nepali (Devanagari).
560567
*/
561568
formatNepali(formatStr: string): string {
562569
return formatNepali(this, formatStr)
@@ -571,6 +578,25 @@ class NepaliDate {
571578
return nepaliDateToString(this)
572579
}
573580

581+
/**
582+
* Returns a string representation (in English) of the English Date in the specified format.
583+
*
584+
* @param {string} formatStr - The format string specifying the desired format.
585+
* @returns {string} The formatted Date string.
586+
*/
587+
formatEnglishDate(formatStr: string): string {
588+
return formatEnglishDate(this, formatStr)
589+
}
590+
591+
/**
592+
* Returns a string representation in the Nepali (Devanagari) of the English Date in the specified format.
593+
* @param formatStr The format string for the desired output.
594+
* @returns {string} The formatted Date string in Nepali (Devanagari).
595+
*/
596+
formatEnglishDateInNepali(formatStr: string): string {
597+
return formatEnglishDateInNepali(this, formatStr)
598+
}
599+
574600
/* Static methods */
575601

576602
/**

src/constants.ts

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const OLD_UTC_OFFSET_IN_MS = 19800000 // 5 hours 40 minutes in ms
1111
export const TIMEZONE_TRANSITION_TIMESTAMP = 504901800000
1212
export const TIMEZONE_TRANSITION_DATE_REFERENCE = new Date(1986, 0, 1, 0, 15)
1313

14-
export const MONTHS_EN = [
14+
export const NEPALI_MONTHS_EN = [
1515
'Baisakh',
1616
'Jestha',
1717
'Asar',
@@ -26,7 +26,7 @@ export const MONTHS_EN = [
2626
'Chaitra',
2727
]
2828

29-
export const MONTHS_SHORT_EN = [
29+
export const NEPALI_MONTHS_SHORT_EN = [
3030
'Bai',
3131
'Jes',
3232
'Asa',
@@ -41,7 +41,7 @@ export const MONTHS_SHORT_EN = [
4141
'Cha',
4242
]
4343

44-
export const MONTHS_NP = [
44+
export const NEPALI_MONTHS_NE = [
4545
'बैशाख',
4646
'जेठ',
4747
'असार',
@@ -56,7 +56,7 @@ export const MONTHS_NP = [
5656
'चैत्र',
5757
]
5858

59-
export const MONTHS_SHORT_NP = [
59+
export const NEPALI_MONTHS_SHORT_NE = [
6060
'बै',
6161
'जे',
6262
'अ',
@@ -71,7 +71,67 @@ export const MONTHS_SHORT_NP = [
7171
'चै',
7272
]
7373

74-
export const NUM_NP = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']
74+
export const ENGLISH_MONTHS_EN = [
75+
'January',
76+
'February',
77+
'March',
78+
'April',
79+
'May',
80+
'June',
81+
'July',
82+
'August',
83+
'September',
84+
'October',
85+
'November',
86+
'December',
87+
]
88+
89+
export const ENGLISH_MONTHS_SHORT_EN = [
90+
'Jan',
91+
'Feb',
92+
'Mar',
93+
'Apr',
94+
'May',
95+
'Jun',
96+
'Jul',
97+
'Aug',
98+
'Sep',
99+
'Oct',
100+
'Nov',
101+
'Dec',
102+
]
103+
104+
export const ENGLISH_MONTHS_NE = [
105+
'जनवरी',
106+
'फेब्रुअरी',
107+
'मार्च',
108+
'अप्रिल',
109+
'मे',
110+
'जुन',
111+
'जुलाई',
112+
'अगस्ट',
113+
'सेप्टेम्बर',
114+
'अक्टोबर',
115+
'नोभेम्बर',
116+
'डिसेम्बर',
117+
]
118+
119+
export const ENGLISH_MONTHS_SHORT_NE = [
120+
'जन',
121+
'फेब',
122+
'मार',
123+
'अप्रि',
124+
'मे',
125+
'जुन',
126+
'जुला',
127+
'अग',
128+
'सेप',
129+
'अक्टो',
130+
'नोभे',
131+
'डिसे',
132+
]
133+
134+
export const NUM_NE = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']
75135

76136
export const WEEKDAYS_SHORT_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
77137

@@ -85,9 +145,9 @@ export const WEEKDAYS_LONG_EN = [
85145
'Saturday',
86146
]
87147

88-
export const WEEKDAYS_SHORT_NP = ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि']
148+
export const WEEKDAYS_SHORT_NE = ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि']
89149

90-
export const WEEKDAYS_LONG_NP = [
150+
export const WEEKDAYS_LONG_NE = [
91151
'आइतबार',
92152
'सोमबार',
93153
'मंगलबार',

0 commit comments

Comments
 (0)