Skip to content

Commit 900d5e0

Browse files
feat: add NepalTimezoneDate class and integrate with NepaliDate
1 parent 8a82798 commit 900d5e0

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ You can create a `NepaliDate` object in several ways:
101101
const date6 = new NepaliDate(2079, 2, 15, 10, 30)
102102
```
103103

104+
- Using a NepalTimezoneDate object: Converts a NepalTimezoneDate object (Gregorian date in Nepal timezone) to a NepaliDate object.
105+
106+
```javascript
107+
const npTzDate = new NepalTimezoneDate('2024-12-28T15:00:35Z')
108+
const nepaliDate = new NepaliDate(npTzDate)
109+
```
110+
104111
#### Getting the Nepali date components
105112

106113
You can retrieve various components of a `NepaliDate` object using the following methods:
@@ -223,6 +230,55 @@ console.log(date2.toString()) // 2080-03-23 10:15:00
223230
- `NepaliDate.minSupportedNepaliDate()`: Returns the minimum supported Nepali object.
224231
- `NepaliDate.maxSupportedNepaliDate()`: Returns the maximum supported Nepali object.
225232

233+
### NepalTimezoneDate
234+
235+
The `NepalTimezoneDate` class provides Gregorian date/time values in Nepal's timezone (Asia/Kathmandu, UTC+05:45).
236+
It works like JavaScript's `Date`, but always returns values as they would appear in Nepal, regardless of your system's timezone.
237+
It does **not** convert to the Nepali calendar.
238+
239+
#### Creating a NepalTimezoneDate object
240+
241+
You can create a `NepalTimezoneDate` object in several ways:
242+
243+
```javascript
244+
import { NepalTimezoneDate } from 'nepali-datetime'
245+
246+
// Current date/time in Nepal timezone
247+
const nowNepal = new NepalTimezoneDate()
248+
249+
// From a UTC date string
250+
const dateNepal = new NepalTimezoneDate('2024-12-28T15:00:35Z')
251+
252+
// From a JS Date object
253+
const jsDate = new Date('2024-12-28T15:00:35Z')
254+
const nepalDate = new NepalTimezoneDate(jsDate)
255+
```
256+
257+
#### Getting Nepal timezone date components
258+
259+
```javascript
260+
dateNepal.getYear() // 2024
261+
dateNepal.getMonth() // 11 (December, 0-based)
262+
dateNepal.getDate() // 28
263+
dateNepal.getHours() // 20
264+
dateNepal.getMinutes() // 45
265+
dateNepal.toString() // "2024-12-28 20:45:35 GMT+0545"
266+
```
267+
268+
#### Comparing with JS Date
269+
270+
```javascript
271+
const systemDate = new Date('2024-12-28T15:00:35Z')
272+
const nepalDate = new NepalTimezoneDate('2024-12-28T15:00:35Z')
273+
274+
// System Date (depends on your computer's timezone)
275+
console.log(systemDate.getHours()) // e.g. 10 (US), 16 (EU), 20 (Nepal)
276+
277+
// NepalTimezoneDate (always Nepal time)
278+
console.log(nepalDate.getHours()) // 20
279+
console.log(nepalDate.toString()) // "2024-12-28 20:45:35 GMT+0545"
280+
```
281+
226282
### dateConverter
227283

228284
The `dateConverter` module provides core functions for converting dates between the Nepali and English calendars.

src/NepalTimezoneDate.ts

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { getNepalDateAndTime } from './utils'
2+
3+
/**
4+
* Represents a Gregorian date/time in Nepal's timezone (Asia/Kathmandu, UTC+05:45).
5+
* Behaves like a JavaScript `Date` object, with all getters returning values in Nepal's timezone.
6+
* Does not convert to the Nepali (Bikram Sambat) calendar.
7+
*
8+
* @example
9+
*
10+
* // Current date/time in Nepal timezone
11+
* const date = new NepalTimezoneDate();
12+
* date.getYear(); // Returns 2025
13+
* date.getMinutes(); // Returns current minutes in Nepal timezone
14+
* date.toString(); // Returns e.g., "2025-09-01 16:01:00 GMT+0545"
15+
*
16+
* @example
17+
*
18+
* // From a specific UTC date
19+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
20+
* date.toString(); // Returns "2024-12-28 20:45:35 GMT+0545"
21+
*/
22+
class NepalTimezoneDate {
23+
private _date: Date
24+
25+
/**
26+
* Creates a NepalTimezoneDate instance for Asia/Kathmandu timezone (UTC+05:45).
27+
* Accepts the same arguments as the JavaScript `Date` constructor.
28+
*
29+
* @param args - Arguments compatible with the `Date` constructor (e.g., timestamp, string, or year/month/day).
30+
* @example
31+
*
32+
* const now = new NepalTimezoneDate(); // Current date/time in Nepal timezone
33+
* const specific = new NepalTimezoneDate(2024, 11, 28, 15, 0, 35); // 2024-12-28 15:00:35 UTC
34+
* const fromString = new NepalTimezoneDate('2024-12-28T15:00:35Z'); // From ISO string
35+
* const fromTimestamp = new NepalTimezoneDate(1703766035170); // From timestamp
36+
*/
37+
constructor(...args: ConstructorParameters<typeof Date>) {
38+
this._date = new Date(...args)
39+
}
40+
41+
/**
42+
* Returns the four-digit year in Nepal timezone (Asia/Kathmandu, UTC+05:45).
43+
*
44+
* @returns The year (e.g., 2024).
45+
* @example
46+
*
47+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
48+
* date.getYear(); // Returns 2024
49+
*/
50+
getYear(): number {
51+
return getNepalDateAndTime(this._date).year
52+
}
53+
54+
/**
55+
* Returns the month index (0-11) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
56+
*
57+
* @returns The month index (0 = January, 11 = December).
58+
* @example
59+
*
60+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
61+
* date.getMonth(); // Returns 11
62+
*/
63+
getMonth(): number {
64+
return getNepalDateAndTime(this._date).month0
65+
}
66+
67+
/**
68+
* Returns the day of the month (1-31) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
69+
*
70+
* @returns The day of the month.
71+
* @example
72+
*
73+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
74+
* date.getDate(); // Returns 28
75+
*/
76+
getDate(): number {
77+
return getNepalDateAndTime(this._date).day
78+
}
79+
80+
/**
81+
* Returns the hour (0-23) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
82+
*
83+
* @returns The hour of the day.
84+
* @example
85+
*
86+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
87+
* date.getHours(); // Returns 20
88+
*/
89+
getHours(): number {
90+
return getNepalDateAndTime(this._date).hour
91+
}
92+
93+
/**
94+
* Returns the minute (0-59) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
95+
*
96+
* @returns The minute of the hour.
97+
* @example
98+
*
99+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
100+
* date.getMinutes(); // Returns 45
101+
*/
102+
getMinutes(): number {
103+
return getNepalDateAndTime(this._date).minute
104+
}
105+
106+
/**
107+
* Returns the second (0-59) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
108+
*
109+
* @returns The second of the minute.
110+
* @example
111+
*
112+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
113+
* date.getSeconds(); // Returns 35
114+
*/
115+
getSeconds(): number {
116+
return getNepalDateAndTime(this._date).second
117+
}
118+
119+
/**
120+
* Returns the millisecond (0-999) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
121+
*
122+
* @returns The millisecond of the second.
123+
* @example
124+
*
125+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35.170Z');
126+
* date.getMilliseconds(); // Returns 170
127+
*/
128+
getMilliseconds(): number {
129+
return getNepalDateAndTime(this._date).ms
130+
}
131+
132+
/**
133+
* Returns the day of the week (0-6, Sunday-Saturday) in Nepal timezone (Asia/Kathmandu, UTC+05:45).
134+
*
135+
* @returns The day of the week (0 = Sunday, 6 = Saturday).
136+
* @example
137+
*
138+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
139+
* date.getDay(); // Returns 6 (Saturday)
140+
*/
141+
getDay(): number {
142+
return getNepalDateAndTime(this._date).weekDay
143+
}
144+
145+
/**
146+
* Returns the Unix timestamp (milliseconds since epoch, UTC).
147+
*
148+
* @returns The timestamp in milliseconds.
149+
* @example
150+
*
151+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
152+
* date.getTime(); // Returns 1703766035000
153+
*/
154+
getTime(): number {
155+
return this._date.getTime()
156+
}
157+
158+
/**
159+
* Returns a formatted string in Nepal timezone (Asia/Kathmandu, UTC+05:45).
160+
* Format: `YYYY-MM-DD HH:mm:ss GMT+0545`
161+
*
162+
* @returns A string representing the date and time in Nepal timezone.
163+
* @example
164+
*
165+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
166+
* date.toString(); // Returns "2024-12-28 20:45:35 GMT+0545"
167+
*/
168+
toString(): string {
169+
const np = getNepalDateAndTime(this._date)
170+
return (
171+
`${np.year}-${String(np.month0 + 1).padStart(2, '0')}-${String(np.day).padStart(2, '0')} ` +
172+
`${String(np.hour).padStart(2, '0')}:${String(np.minute).padStart(2, '0')}:${String(np.second).padStart(2, '0')} GMT+0545`
173+
)
174+
}
175+
176+
/**
177+
* Returns a copy of the underlying JavaScript `Date` object.
178+
*
179+
* @returns A new `Date` instance with the same timestamp.
180+
* @example
181+
*
182+
* const date = new NepalTimezoneDate('2024-12-28T15:00:35Z');
183+
* const jsDate = date.toDate(); // Returns Date object for 2024-12-28T15:00:35Z
184+
*/
185+
toDate(): Date {
186+
return new Date(this._date)
187+
}
188+
}
189+
190+
export default NepalTimezoneDate

src/NepaliDate.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
formatNepali,
88
nepaliDateToString,
99
} from './format'
10+
import NepalTimezoneDate from './NepalTimezoneDate'
1011

1112
import { parse, parseFormat, parseEnglishDateFormat } from './parse'
1213
import { getDate, getNepalDateAndTime } from './utils'
@@ -119,6 +120,17 @@ class NepaliDate {
119120
second?: number,
120121
ms?: number
121122
)
123+
124+
/**
125+
* Creates a NepaliDate instance from a NepalTimezoneDate object.
126+
*
127+
* @param {NepalTimezoneDate} date - The NepalTimezoneDate object.
128+
* @example
129+
* const npTzDate = new NepalTimezoneDate('2024-12-28T15:00:35Z')
130+
* const nepaliDate = new NepaliDate(npTzDate)
131+
*/
132+
constructor(date: NepalTimezoneDate)
133+
122134
constructor(...args: any[]) {
123135
if (args.length === 0) {
124136
this.initFromCurrentDate()
@@ -130,6 +142,8 @@ class NepaliDate {
130142
this.parseFromString(args[0])
131143
} else if (args.length === 1 && typeof args[0] === 'number') {
132144
this.initFromTimestamp(args[0])
145+
} else if (args.length === 1 && args[0] instanceof NepalTimezoneDate) {
146+
this._setDateObject(args[0].toDate())
133147
} else if (
134148
args.length === 2 &&
135149
typeof args[0] === 'string' &&

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import NepaliDate from './NepaliDate'
2+
import NepalTimezoneDate from './NepalTimezoneDate'
23

34
export default NepaliDate
5+
export { NepalTimezoneDate }

0 commit comments

Comments
 (0)