Skip to content

Commit d370c23

Browse files
Merge branch 'main' into nepal-timezone-date-class
2 parents 92624cf + 4e91481 commit d370c23

File tree

12 files changed

+113
-22
lines changed

12 files changed

+113
-22
lines changed

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{".":"1.4.1"}
1+
{".":"1.5.0"}

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## [1.5.0](https://github.com/opensource-nepal/node-nepali-datetime/compare/v1.4.1...v1.5.0) (2025-09-03)
4+
5+
6+
### Features
7+
8+
* add parseNepaliFormat method on NepaliDate ([#111](https://github.com/opensource-nepal/node-nepali-datetime/issues/111)) ([ff4a05b](https://github.com/opensource-nepal/node-nepali-datetime/commit/ff4a05b6e2ce09d8ffa31a56a64b4fce54a2618b))
9+
310
## [1.4.1](https://github.com/opensource-nepal/node-nepali-datetime/compare/v1.4.0...v1.4.1) (2025-04-08)
411

512

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ For the list of month names and their abbreviations, you can refer to the table
194194
| 10 | Fal | Falgun |
195195
| 11 | Cha | Chaitra |
196196

197+
#### Parsing Dates
198+
199+
You can parse a date-time string into a `NepaliDate` object using the following methods:
200+
201+
- `new NepaliDate(dateString, formatStr)`: Parses an Nepali date-time string according to the specified format.
202+
- `NepaliDate.parseNepaliFormat(dateStringNe, formatStr)`: Parses a Nepali date-time string in Devanagari form according to the specified format.
203+
- `NepaliDate.parseEnglishDate(dateString, formatStr)`: Parses an English date-time string according to the specified format.
204+
197205
#### Converting to JavaScript Date object
198206

199207
You can get the equivalent JavaScript `Date` object of a `NepaliDate` object using the `getDateObject()` method.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nepali-datetime",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "A Node project designed to support native JavaScript-like features for Nepali dates. It includes features such as 'NepaliDate' for Nepali date support and 'dateConverter' for date conversions.",
55
"exports": {
66
".": {

src/NepaliDate.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
} from './format'
1010
import NepalTimezoneDate from './NepalTimezoneDate'
1111

12-
import { parse, parseFormat, parseEnglishDateFormat } from './parse'
12+
import {
13+
simpleParse,
14+
parseFormat,
15+
parseEnglishDateFormat,
16+
parseNepaliFormat,
17+
} from './parse'
1318
import { validateTime } from './validators'
1419

1520
/**
@@ -183,7 +188,7 @@ class NepaliDate {
183188
}
184189

185190
private parseFromString(value: string) {
186-
const parsedResult = parse(value)
191+
const parsedResult = simpleParse(value)
187192
this.set(
188193
parsedResult[0], // Year
189194
parsedResult[1], // Month
@@ -263,7 +268,7 @@ class NepaliDate {
263268
*
264269
* @returns {Date} The equivalent JavaScript Date object.
265270
*/
266-
getDateObject() {
271+
getDateObject(): Date {
267272
return this.timestamp
268273
}
269274

@@ -653,8 +658,27 @@ class NepaliDate {
653658
}
654659

655660
/**
656-
* Creates a NepaliDate instance by parsing a provided English Date and Time string
657-
* with the given format.
661+
* Parses a Nepali date-time string in Devanagari form according to the
662+
* specified format and returns a `NepaliDate` instance.
663+
*
664+
* @param dateStringNe - The Nepali Date and time string in Devanagari.
665+
* @param format - The format of the provided date-time string.
666+
* @example
667+
* const dateStringNe = '२०८०/०८/१२ १४-०५-२३.७८९'
668+
* const format = 'YYYY/MM/DD HH-mm-ss.SSS'
669+
* const nepaliDate = NepaliDate.parseNepaliFormat(dateStringNe, format)
670+
*/
671+
static parseNepaliFormat(dateStringNe: string, format: string): NepaliDate {
672+
const [year, month0, day, hour, minute, second, ms] = parseNepaliFormat(
673+
dateStringNe,
674+
format
675+
)
676+
return new NepaliDate(year, month0, day, hour, minute, second, ms)
677+
}
678+
679+
/**
680+
* Parses an English date-time string according to the specified format
681+
* and returns a `NepaliDate` instance.
658682
*
659683
* @param dateString - The English Date and time string.
660684
* @param format - The format of the provided date-time string.

src/parse/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { parseFormat, parse } from './parse'
1+
export { parseFormat, simpleParse } from './parse'
22
export { parseEnglishDateFormat } from './parseEnglishDate'
3+
export { parseNepaliFormat } from './parseNepaliFormat'

src/parse/parse.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
/**
2-
* parse.ts
3-
*
4-
* This module provides methods for parsing dates and times from strings.
5-
*
6-
* Functions:
7-
*
8-
* parse(dateTimeString)
9-
* - Parses date and time from the given string.
10-
*
11-
* Further extension is needed in this module as there are limited formats supported for parsing.
12-
* Developers should consider extending the module to support additional date and time formats.
2+
* This module provides features for parsing Nepali Date strings (in English).
133
*/
144

155
import {
@@ -92,7 +82,7 @@ function parseTimeString(timeString: string): number[] {
9282
* @throws {Error} if date or time string is invalid
9383
* @returns return array of date information [hour, minute, second, ms].
9484
*/
95-
export function parse(dateTimeString: string): number[] {
85+
export function simpleParse(dateTimeString: string): number[] {
9686
const [dateString, timeString] = dateTimeString.split(' ', 2)
9787
const [year, month0, day] = parseDateString(dateString)
9888
const [hour, minute, second, ms] = parseTimeString(timeString)

src/parse/parseEnglishDate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* This module provides methods for parsing English Date strings.
3+
*/
4+
15
import {
26
ENGLISH_MONTHS_EN,
37
ENGLISH_MONTHS_SHORT_EN,

src/parse/parseNepaliFormat.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* This module provides methods for parsing Nepali Date strings (in Devanagari).
3+
*/
4+
5+
import {
6+
NEPALI_MONTHS_EN,
7+
NEPALI_MONTHS_NE,
8+
NUM_NE,
9+
WEEKDAYS_LONG_EN,
10+
WEEKDAYS_LONG_NE,
11+
WEEKDAYS_SHORT_EN,
12+
WEEKDAYS_SHORT_NE,
13+
} from '../constants'
14+
import { parseFormat } from './parse'
15+
16+
function convertNepaliStringToEnglish(dateStringNe: string): string {
17+
const regexLookup: Record<string, string> = {
18+
एम: 'AM',
19+
पिम: 'PM',
20+
}
21+
NEPALI_MONTHS_NE.forEach((ne, i) => (regexLookup[ne] = NEPALI_MONTHS_EN[i]))
22+
WEEKDAYS_LONG_NE.forEach((ne, i) => (regexLookup[ne] = WEEKDAYS_LONG_EN[i]))
23+
WEEKDAYS_SHORT_NE.forEach((ne, i) => (regexLookup[ne] = WEEKDAYS_SHORT_EN[i]))
24+
NUM_NE.forEach((ne, i) => (regexLookup[ne] = String(i)))
25+
26+
// create a regex pattern
27+
const pattern = new RegExp(Object.keys(regexLookup).join('|'), 'g')
28+
29+
// replace the match
30+
return dateStringNe.replace(pattern, match => regexLookup[match])
31+
}
32+
33+
export function parseNepaliFormat(dateStringNe: string, format: string): number[] {
34+
const convertedDateString = convertNepaliStringToEnglish(dateStringNe)
35+
return parseFormat(convertedDateString, format)
36+
}

0 commit comments

Comments
 (0)