Skip to content

Commit

Permalink
feat: add method formatEnglishDate and formatEnglishDateInNepali (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3sh authored Nov 22, 2024
1 parent 29cda16 commit 6cf9d14
Show file tree
Hide file tree
Showing 12 changed files with 1,049 additions and 318 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,17 @@ You can set individual components of a `NepaliDate` object using the following m
#### Formatting the Nepali date

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

- `format(formatStr)`: Returns a string representation (in English) of the `NepaliDate` object in the specified format.
- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari) script of the `NepaliDate` object in the specified format.
- `formatNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the `NepaliDate` object in the specified format.
- `formatEnglishDate(formatStr)`: Returns a string representation (in English) of the English Date in the specified format.
- `formatEnglishDateInNepali(formatStr)`: Returns a string representation in the Nepali (Devanagari script) of the English Date in the specified format.

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

The date formatting will follow the format codes mentioned below, which are similar to the date formats used in day.js.
Expand Down
30 changes: 28 additions & 2 deletions src/NepaliDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import dateConverter from './dateConverter'
import { format, formatNepali, nepaliDateToString } from './format'
import {
format,
formatEnglishDate,
formatEnglishDateInNepali,
formatNepali,
nepaliDateToString,
} from './format'

import { parse, parseFormat } from './parse'
import { getDate, getNepalDateAndTime } from './utils'
import { validateTime } from './validators'
Expand Down Expand Up @@ -556,7 +563,7 @@ class NepaliDate {
/**
* Returns a string representation in the Nepali (Devanagari) of the NepaliDate object in the specified format.
* @param formatStr The format string for the desired output.
* @returns {string} A string representation of the NepaliDate object in the specified format.
* @returns {string} The formatted Date string in Nepali (Devanagari).
*/
formatNepali(formatStr: string): string {
return formatNepali(this, formatStr)
Expand All @@ -571,6 +578,25 @@ class NepaliDate {
return nepaliDateToString(this)
}

/**
* Returns a string representation (in English) of the English Date in the specified format.
*
* @param {string} formatStr - The format string specifying the desired format.
* @returns {string} The formatted Date string.
*/
formatEnglishDate(formatStr: string): string {
return formatEnglishDate(this, formatStr)
}

/**
* Returns a string representation in the Nepali (Devanagari) of the English Date in the specified format.
* @param formatStr The format string for the desired output.
* @returns {string} The formatted Date string in Nepali (Devanagari).
*/
formatEnglishDateInNepali(formatStr: string): string {
return formatEnglishDateInNepali(this, formatStr)
}

/* Static methods */

/**
Expand Down
74 changes: 67 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const OLD_UTC_OFFSET_IN_MS = 19800000 // 5 hours 40 minutes in ms
export const TIMEZONE_TRANSITION_TIMESTAMP = 504901800000
export const TIMEZONE_TRANSITION_DATE_REFERENCE = new Date(1986, 0, 1, 0, 15)

export const MONTHS_EN = [
export const NEPALI_MONTHS_EN = [
'Baisakh',
'Jestha',
'Asar',
Expand All @@ -26,7 +26,7 @@ export const MONTHS_EN = [
'Chaitra',
]

export const MONTHS_SHORT_EN = [
export const NEPALI_MONTHS_SHORT_EN = [
'Bai',
'Jes',
'Asa',
Expand All @@ -41,7 +41,7 @@ export const MONTHS_SHORT_EN = [
'Cha',
]

export const MONTHS_NP = [
export const NEPALI_MONTHS_NE = [
'बैशाख',
'जेठ',
'असार',
Expand All @@ -56,7 +56,7 @@ export const MONTHS_NP = [
'चैत्र',
]

export const MONTHS_SHORT_NP = [
export const NEPALI_MONTHS_SHORT_NE = [
'बै',
'जे',
'अ',
Expand All @@ -71,7 +71,67 @@ export const MONTHS_SHORT_NP = [
'चै',
]

export const NUM_NP = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']
export const ENGLISH_MONTHS_EN = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]

export const ENGLISH_MONTHS_SHORT_EN = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]

export const ENGLISH_MONTHS_NE = [
'जनवरी',
'फेब्रुअरी',
'मार्च',
'अप्रिल',
'मे',
'जुन',
'जुलाई',
'अगस्ट',
'सेप्टेम्बर',
'अक्टोबर',
'नोभेम्बर',
'डिसेम्बर',
]

export const ENGLISH_MONTHS_SHORT_NE = [
'जन',
'फेब',
'मार',
'अप्रि',
'मे',
'जुन',
'जुला',
'अग',
'सेप',
'अक्टो',
'नोभे',
'डिसे',
]

export const NUM_NE = ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९']

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

Expand All @@ -85,9 +145,9 @@ export const WEEKDAYS_LONG_EN = [
'Saturday',
]

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

export const WEEKDAYS_LONG_NP = [
export const WEEKDAYS_LONG_NE = [
'आइतबार',
'सोमबार',
'मंगलबार',
Expand Down
Loading

0 comments on commit 6cf9d14

Please sign in to comment.