@ma.vu/appdate 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +268 -0
- package/dist/index.d.ts +268 -0
- package/dist/index.js +2 -0
- package/package.json +4 -2
package/README.md
CHANGED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";var T=Object.create;var D=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var w=(a,t)=>{for(var e in t)D(a,e,{get:t[e],enumerable:!0})},c=(a,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of L(t))!S.call(a,o)&&o!==e&&D(a,o,{get:()=>t[o],enumerable:!(i=j(t,o))||i.enumerable});return a};var s=(a,t,e)=>(e=a!=null?T(M(a)):{},c(t||!a||!a.__esModule?D(e,"default",{value:a,enumerable:!0}):e,a)),Y=a=>c(D({},"__esModule",{value:!0}),a);var k={};w(k,{AppDate:()=>d,formatLocalTime:()=>b,getLocalizedDateString:()=>z,isDateString:()=>h,setAppDateLanguage:()=>O,setTimezone:()=>A});module.exports=Y(k);var r=s(require("dayjs"),1),E=require("dayjs/locale/de"),p=s(require("dayjs/plugin/customParseFormat.js"),1),l=s(require("dayjs/plugin/isBetween.js"),1),y=s(require("dayjs/plugin/localizedFormat.js"),1),f=s(require("dayjs/plugin/timezone.js"),1),g=s(require("dayjs/plugin/utc.js"),1);r.default.extend(g.default);r.default.extend(f.default);r.default.extend(p.default);r.default.extend(l.default);r.default.extend(y.default);async function O(a){switch(a){case"de":{let t=await import("dayjs/locale/de-ch");r.default.locale(t.default);break}case"fr":{let t=await import("dayjs/locale/fr-ch");r.default.locale(t.default);break}case"en":default:{let t=await import("dayjs/locale/en");r.default.locale(t.default);break}}}var n="Europe/Zurich";function A(a){n=a}var u="HH:mm",m="HH:mm:ssZ",d=class a{timezone;dayjsDate;static INVALID_DATE=(0,r.default)("");constructor(t,e,{invalid:i}={invalid:!1}){if(this.timezone=t,i){this.dayjsDate=a.INVALID_DATE;return}try{if(typeof e=="string"&&!h(e))throw new Error("Invalid Date string, we expect YYYY-DD-MM");this.dayjsDate=r.default.tz(e,t)}catch{console.warn("Could not parse date:",e),this.dayjsDate=a.INVALID_DATE}}static invalid(){return new a(n,"",{invalid:!0})}static now(){return new a(n,(0,r.default)())}static fromDateString(t){return new a(n,t)}static fromEpochSeconds(t){let e=r.default.unix(t);return new a(n,e)}static fromEpochMillis(t){let e=(0,r.default)(t);return new a(n,e)}static fromLocalTime(t){try{let e=r.default.tz(t,u,n);return new a(n,e)}catch(e){return e instanceof Error&&console.warn(`fromLocalTime(): ${e.message}`),a.invalid()}}static fromUtcString(t){let e=r.default.utc(t);return new a(n,e)}static fromUtcTime(t){let e=r.default.utc(t,m);return new a(n,e)}static minDate(){return a.fromDateString("1900-01-01")}static maxDate(){return a.fromDateString("2200-12-31")}add(t,e){let i=this.dayjsDate.add(t,e);return new a(n,i)}subtract(t,e){let i=this.dayjsDate.subtract(t,e);return new a(n,i)}startOf(t){let e=this.dayjsDate.startOf(t);return new a(n,e)}endOf(t){let e=this.dayjsDate.endOf(t);return new a(n,e)}tomorrow(){return this.add(1,"day")}isValid(){return this.dayjsDate.isValid()}isBefore(t,e){return this.dayjsDate.isBefore(t.dayjsDate,e)}isSame(t,e){return this.dayjsDate.isSame(t.dayjsDate,e)}isToday(){return this.dayjsDate.endOf("d").isSame((0,r.default)().endOf("d"))}isAfter(t,e){return this.dayjsDate.isAfter(t.dayjsDate,e)}isBetween(t=a.minDate(),e=a.maxDate(),i,o){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,i,o??"[)")}isFirstDayOfWeek(){return this.dayjsDate.day()===1}isWorkingDay(){return x.includes(this.dayjsDate.day())}nextWorkingDay(){let t=this.add(1,"day");return t.isWorkingDay()?t:t.nextWorkingDay()}previousWorkingDay(){let t=this.subtract(1,"day");return t.isWorkingDay()?t:t.previousWorkingDay()}addWorkingDays(t){return t<=0||!Number.isInteger(t)?this:this.nextWorkingDay().addWorkingDays(t-1)}toLocalTime(){return this.format(u)}toUtcTime(){return this.dayjsDate.utc().format(m)}toDateString(){return this.format("YYYY-MM-DD")}toLocalizedDateString({includeDayOfWeek:t=!1}={}){let e=this.dayjsDate.format("L");return t?this.dayjsDate.format("dd, ")+e:e}toUtcDateString(){return this.dayjsDate.utc().format("YYYY-MM-DD")}toUtcString(){return this.dayjsDate.utc().format()}toEpochSeconds(){return this.dayjsDate.unix()}toEpochMillis(){return this.dayjsDate.valueOf()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){return t?this.format("dd, DD.MM."):this.format("DD.MM.")}formatDateTime({includeDayOfWeek:t=!0}={}){let e=this.toLocalizedDateString({includeDayOfWeek:t}),i=this.toLocalTime();return`${e}, ${i}`}},x=[1,2,3,4,5];function z(a,t){return d.fromDateString(a).toLocalizedDateString(t)}function b(a){return d.fromLocalTime(a).toLocalTime()}function h(a){return a?(0,r.default)(a,"YYYY-MM-DD",!0).isValid():!1}0&&(module.exports={AppDate,formatLocalTime,getLocalizedDateString,isDateString,setAppDateLanguage,setTimezone});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Dayjs, ManipulateType, OpUnitType } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given language string, formaters, months, weeks
|
|
5
|
+
* will be localized to provided language
|
|
6
|
+
* de: 10.10.2010
|
|
7
|
+
* en: 10/10/2010
|
|
8
|
+
*/
|
|
9
|
+
declare function setAppDateLanguage(lang: "de" | "en" | "fr"): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Change zone in runtime
|
|
12
|
+
*/
|
|
13
|
+
declare function setTimezone(timezone: string): void;
|
|
14
|
+
type DateString = `${string}-${string}-${string}`;
|
|
15
|
+
/**
|
|
16
|
+
* AppDate: A timezone-aware date and time abstraction.
|
|
17
|
+
*
|
|
18
|
+
* IMPORTANT: Always use this wrapped abstraction instead of moment or
|
|
19
|
+
* new Date directly. You should not polute the application with randomly used
|
|
20
|
+
* date methods.
|
|
21
|
+
*
|
|
22
|
+
* This class wraps the dayjs library to provide a consistent interface for
|
|
23
|
+
* working with dates and times in a specific timezone. It offers methods for
|
|
24
|
+
* date manipulation, comparison, and formatting, while maintaining timezone
|
|
25
|
+
* context.
|
|
26
|
+
*
|
|
27
|
+
* Key features:
|
|
28
|
+
* - Timezone awareness: All operations respect the specified timezone.
|
|
29
|
+
* - Immutability: Operations return new instances, preserving the original.
|
|
30
|
+
* - Consistent API: Provides a uniform interface for date operations.
|
|
31
|
+
* - Error handling: Gracefully handles invalid dates and parsing errors.
|
|
32
|
+
*
|
|
33
|
+
* Use AppDate to ensure consistent date handling across your application,
|
|
34
|
+
* especially when dealing with different timezones or complex date logic.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const today = AppDate.now();
|
|
38
|
+
* const futureDate = today.add(5, 'days');
|
|
39
|
+
* console.log(futureDate.toLocalizedDateString());
|
|
40
|
+
*/
|
|
41
|
+
declare class AppDate {
|
|
42
|
+
readonly timezone: string;
|
|
43
|
+
readonly dayjsDate: Dayjs;
|
|
44
|
+
private static readonly INVALID_DATE;
|
|
45
|
+
/**
|
|
46
|
+
* constructor is private, so new LocalString("something")
|
|
47
|
+
* outside class is not posible
|
|
48
|
+
*/
|
|
49
|
+
private constructor();
|
|
50
|
+
/**
|
|
51
|
+
* Creates an invalid AppDate instance.
|
|
52
|
+
* Usiful for: Error handeling, default values, avoids
|
|
53
|
+
* null object pattern and plays nicely with validation
|
|
54
|
+
*/
|
|
55
|
+
static invalid(): AppDate;
|
|
56
|
+
static now(): AppDate;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a AppDate instance from a date string.
|
|
59
|
+
*
|
|
60
|
+
* @param date - A string representing a date in "YYYY-MM-DD" format.
|
|
61
|
+
* @returns A new AppDate instance set to the given date.
|
|
62
|
+
*
|
|
63
|
+
* If the date string is invalid or cannot be parsed, it returns an invalid AppDate instance.
|
|
64
|
+
* The time part of the created AppDate will be set to midnight in the local timezone.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const date = AppDate.fromDateString("2023-05-21");
|
|
68
|
+
*/
|
|
69
|
+
static fromDateString(date: string): AppDate;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a AppDate instance from a epoch seconds.
|
|
72
|
+
*
|
|
73
|
+
* @param seconds - A number representing a epoch seconds.
|
|
74
|
+
* @returns A new AppDate instance set to the given epoch seconds.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const date = AppDate.fromEpochSeconds(1714732800);
|
|
79
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static fromEpochSeconds(seconds: number): AppDate;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a AppDate instance from a epoch milliseconds.
|
|
85
|
+
*
|
|
86
|
+
* @param ms - A number representing a epoch milliseconds.
|
|
87
|
+
* @returns A new AppDate instance set to the given epoch milliseconds.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const date = AppDate.fromEpochMillis(1714732800000);
|
|
92
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
93
|
+
* ```
|
|
94
|
+
}
|
|
95
|
+
*/
|
|
96
|
+
static fromEpochMillis(ms: number): AppDate;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a AppDate instance from a local time string.
|
|
99
|
+
*
|
|
100
|
+
* @param time - A string representing a local time in any valid (24h) time format.
|
|
101
|
+
* @returns A new AppDate instance set to the given time on the todays date.
|
|
102
|
+
*
|
|
103
|
+
* If the time string is invalid, it returns an invalid AppDate instance.
|
|
104
|
+
* The date part defaults to the current date in the local timezone.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const date = AppDate.fromLocalTime("14:30"); Today's date, 14:30 or (02:30 PM)
|
|
108
|
+
*/
|
|
109
|
+
static fromLocalTime(time: string): AppDate;
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @param date - A string representing a UTC date in "YYYY-MM-DD" format.
|
|
113
|
+
* @returns A new AppDate instance set to the given UTC date.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const date = AppDate.fromUtcString("2026-01-04");
|
|
117
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
static fromUtcString(date?: string): AppDate;
|
|
121
|
+
/**
|
|
122
|
+
* Creates a AppDate instance from a UTC time string.
|
|
123
|
+
*
|
|
124
|
+
* @param time - A string representing a UTC time in "HH:mm:ssZ" format.
|
|
125
|
+
* @returns A new AppDate instance set to the given UTC time.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const time = AppDate.fromUtcTime("14:30:00+00:00");
|
|
129
|
+
* console.log(time.toLocalizedDateString()); // "04.01.2026"
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
static fromUtcTime(time: string): AppDate;
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @returns A new AppDate instance set to the minimum supported date (1900-01-01).
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* const minDate = AppDate.minDate();
|
|
139
|
+
* console.log(minDate.toLocalizedDateString()); // "01.01.1900"
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
static minDate(): AppDate;
|
|
143
|
+
/**
|
|
144
|
+
* Returns the maximum supported date (2200-12-31).
|
|
145
|
+
*
|
|
146
|
+
* @returns A new AppDate instance set to the maximum supported date.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* const maxDate = AppDate.maxDate();
|
|
150
|
+
* console.log(maxDate.toLocalizedDateString()); // "31.12.2200"
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
static maxDate(): AppDate;
|
|
154
|
+
add(value: number, unit?: ManipulateType): AppDate;
|
|
155
|
+
subtract(value: number, unit?: ManipulateType): AppDate;
|
|
156
|
+
startOf(unit: OpUnitType): AppDate;
|
|
157
|
+
endOf(unit: OpUnitType): AppDate;
|
|
158
|
+
tomorrow(): AppDate;
|
|
159
|
+
isValid(): boolean;
|
|
160
|
+
isBefore(other: AppDate, unit?: OpUnitType): boolean;
|
|
161
|
+
isSame(other: AppDate, unit?: OpUnitType): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* returns true if date is current day
|
|
164
|
+
*/
|
|
165
|
+
isToday(): boolean;
|
|
166
|
+
isAfter(other: AppDate, unit?: OpUnitType): boolean;
|
|
167
|
+
isBetween(from?: AppDate, to?: AppDate, unit?: OpUnitType, inclusivity?: `${"(" | "["}${")" | "]"}`): boolean;
|
|
168
|
+
isFirstDayOfWeek(): boolean;
|
|
169
|
+
isWorkingDay(): boolean;
|
|
170
|
+
nextWorkingDay(): AppDate;
|
|
171
|
+
previousWorkingDay(): AppDate;
|
|
172
|
+
addWorkingDays(days: number): AppDate;
|
|
173
|
+
/*** Formatters ***/
|
|
174
|
+
/**
|
|
175
|
+
* Converts Date in following string format: HH:mm (20:10)
|
|
176
|
+
*/
|
|
177
|
+
toLocalTime(): string;
|
|
178
|
+
/**
|
|
179
|
+
* Returns time in UTC format: HH:mm:ssZ
|
|
180
|
+
*/
|
|
181
|
+
toUtcTime(): string;
|
|
182
|
+
/**
|
|
183
|
+
* Converts the current date to a string: YYYY-MM-DD
|
|
184
|
+
*
|
|
185
|
+
* @returns A string representing the current date in "YYYY-MM-DD" format.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* const date = AppDate.now();
|
|
189
|
+
* console.log(date.toDateString()); // "2026-01-04"
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
toDateString(): DateString;
|
|
193
|
+
/**
|
|
194
|
+
* Locale friendly format:
|
|
195
|
+
* ```
|
|
196
|
+
* de, ch = 20.10.1985
|
|
197
|
+
* us = 10/20/1985
|
|
198
|
+
* {includeDayOfWeek: true}
|
|
199
|
+
* de, ch = Son, 20.10.1985
|
|
200
|
+
* us = Sun, 10/20/1985
|
|
201
|
+
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
toLocalizedDateString({ includeDayOfWeek, }?: LocalizedFormatOptions): string;
|
|
205
|
+
toUtcDateString(): DateString;
|
|
206
|
+
toUtcString(): string;
|
|
207
|
+
/**
|
|
208
|
+
* Converts the current date to a epoch seconds (unix timestamp).
|
|
209
|
+
*
|
|
210
|
+
* @returns The epoch seconds.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* const date = AppDate.now();
|
|
214
|
+
* console.log(date.toEpochSeconds()); // 1714732800
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
toEpochSeconds(): number;
|
|
218
|
+
/**
|
|
219
|
+
* Converts the current date to a epoch milliseconds (timestamp in milliseconds).
|
|
220
|
+
*
|
|
221
|
+
* @returns The epoch milliseconds.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* const date = AppDate.now();
|
|
225
|
+
* console.log(date.toEpochMillis()); // 1714732800000
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
toEpochMillis(): number;
|
|
229
|
+
/**
|
|
230
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
231
|
+
*
|
|
232
|
+
* To escape characters, wrap them in square brackets (e.g. [MM]).
|
|
233
|
+
*
|
|
234
|
+
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
235
|
+
*
|
|
236
|
+
*/
|
|
237
|
+
format(template?: FormatTemplate): string;
|
|
238
|
+
/**
|
|
239
|
+
*
|
|
240
|
+
* @param options - Optional settings for short formatting.
|
|
241
|
+
* @returns The short formatted date string.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* const date = AppDate.now();
|
|
245
|
+
* console.log(date.formatShort({ includeDayOfWeek: true })); // "Su, 04.01."
|
|
246
|
+
* console.log(date.formatShort({ includeDayOfWeek: false })); // "04.01."
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
250
|
+
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
251
|
+
}
|
|
252
|
+
interface LocalizedFormatOptions {
|
|
253
|
+
includeDayOfWeek?: boolean;
|
|
254
|
+
}
|
|
255
|
+
interface GetNDaysOptions {
|
|
256
|
+
startDate?: AppDate;
|
|
257
|
+
excludeStartDate?: boolean;
|
|
258
|
+
}
|
|
259
|
+
declare function getLocalizedDateString(date: string, options?: LocalizedFormatOptions): string;
|
|
260
|
+
declare function formatLocalTime(time: string): string;
|
|
261
|
+
/**
|
|
262
|
+
* returns true if given argument is in YYYY-MM-DD format
|
|
263
|
+
* otherwise false
|
|
264
|
+
*/
|
|
265
|
+
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
266
|
+
type FormatTemplate = "YY" | "YYYY" | "M" | "MM" | "MMM" | "MMMM" | "D" | "DD" | "d" | "dd" | "ddd" | "dddd" | "H" | "HH" | "h" | "hh" | "m" | "mm" | "s" | "ss" | "SSS" | "Z" | "ZZ" | "A" | "a" | "LT" | "LTS" | /* en: 10/10/2020, de: 10.10.2020 */ "L" | "LL" | "LLL" | "LLLL" | "l" | "ll" | "lll" | "llll" | (string & {});
|
|
267
|
+
|
|
268
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Dayjs, ManipulateType, OpUnitType } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given language string, formaters, months, weeks
|
|
5
|
+
* will be localized to provided language
|
|
6
|
+
* de: 10.10.2010
|
|
7
|
+
* en: 10/10/2010
|
|
8
|
+
*/
|
|
9
|
+
declare function setAppDateLanguage(lang: "de" | "en" | "fr"): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Change zone in runtime
|
|
12
|
+
*/
|
|
13
|
+
declare function setTimezone(timezone: string): void;
|
|
14
|
+
type DateString = `${string}-${string}-${string}`;
|
|
15
|
+
/**
|
|
16
|
+
* AppDate: A timezone-aware date and time abstraction.
|
|
17
|
+
*
|
|
18
|
+
* IMPORTANT: Always use this wrapped abstraction instead of moment or
|
|
19
|
+
* new Date directly. You should not polute the application with randomly used
|
|
20
|
+
* date methods.
|
|
21
|
+
*
|
|
22
|
+
* This class wraps the dayjs library to provide a consistent interface for
|
|
23
|
+
* working with dates and times in a specific timezone. It offers methods for
|
|
24
|
+
* date manipulation, comparison, and formatting, while maintaining timezone
|
|
25
|
+
* context.
|
|
26
|
+
*
|
|
27
|
+
* Key features:
|
|
28
|
+
* - Timezone awareness: All operations respect the specified timezone.
|
|
29
|
+
* - Immutability: Operations return new instances, preserving the original.
|
|
30
|
+
* - Consistent API: Provides a uniform interface for date operations.
|
|
31
|
+
* - Error handling: Gracefully handles invalid dates and parsing errors.
|
|
32
|
+
*
|
|
33
|
+
* Use AppDate to ensure consistent date handling across your application,
|
|
34
|
+
* especially when dealing with different timezones or complex date logic.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const today = AppDate.now();
|
|
38
|
+
* const futureDate = today.add(5, 'days');
|
|
39
|
+
* console.log(futureDate.toLocalizedDateString());
|
|
40
|
+
*/
|
|
41
|
+
declare class AppDate {
|
|
42
|
+
readonly timezone: string;
|
|
43
|
+
readonly dayjsDate: Dayjs;
|
|
44
|
+
private static readonly INVALID_DATE;
|
|
45
|
+
/**
|
|
46
|
+
* constructor is private, so new LocalString("something")
|
|
47
|
+
* outside class is not posible
|
|
48
|
+
*/
|
|
49
|
+
private constructor();
|
|
50
|
+
/**
|
|
51
|
+
* Creates an invalid AppDate instance.
|
|
52
|
+
* Usiful for: Error handeling, default values, avoids
|
|
53
|
+
* null object pattern and plays nicely with validation
|
|
54
|
+
*/
|
|
55
|
+
static invalid(): AppDate;
|
|
56
|
+
static now(): AppDate;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a AppDate instance from a date string.
|
|
59
|
+
*
|
|
60
|
+
* @param date - A string representing a date in "YYYY-MM-DD" format.
|
|
61
|
+
* @returns A new AppDate instance set to the given date.
|
|
62
|
+
*
|
|
63
|
+
* If the date string is invalid or cannot be parsed, it returns an invalid AppDate instance.
|
|
64
|
+
* The time part of the created AppDate will be set to midnight in the local timezone.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const date = AppDate.fromDateString("2023-05-21");
|
|
68
|
+
*/
|
|
69
|
+
static fromDateString(date: string): AppDate;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a AppDate instance from a epoch seconds.
|
|
72
|
+
*
|
|
73
|
+
* @param seconds - A number representing a epoch seconds.
|
|
74
|
+
* @returns A new AppDate instance set to the given epoch seconds.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const date = AppDate.fromEpochSeconds(1714732800);
|
|
79
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
static fromEpochSeconds(seconds: number): AppDate;
|
|
83
|
+
/**
|
|
84
|
+
* Creates a AppDate instance from a epoch milliseconds.
|
|
85
|
+
*
|
|
86
|
+
* @param ms - A number representing a epoch milliseconds.
|
|
87
|
+
* @returns A new AppDate instance set to the given epoch milliseconds.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const date = AppDate.fromEpochMillis(1714732800000);
|
|
92
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
93
|
+
* ```
|
|
94
|
+
}
|
|
95
|
+
*/
|
|
96
|
+
static fromEpochMillis(ms: number): AppDate;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a AppDate instance from a local time string.
|
|
99
|
+
*
|
|
100
|
+
* @param time - A string representing a local time in any valid (24h) time format.
|
|
101
|
+
* @returns A new AppDate instance set to the given time on the todays date.
|
|
102
|
+
*
|
|
103
|
+
* If the time string is invalid, it returns an invalid AppDate instance.
|
|
104
|
+
* The date part defaults to the current date in the local timezone.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const date = AppDate.fromLocalTime("14:30"); Today's date, 14:30 or (02:30 PM)
|
|
108
|
+
*/
|
|
109
|
+
static fromLocalTime(time: string): AppDate;
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @param date - A string representing a UTC date in "YYYY-MM-DD" format.
|
|
113
|
+
* @returns A new AppDate instance set to the given UTC date.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const date = AppDate.fromUtcString("2026-01-04");
|
|
117
|
+
* console.log(date.toLocalizedDateString()); // "04.01.2026"
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
static fromUtcString(date?: string): AppDate;
|
|
121
|
+
/**
|
|
122
|
+
* Creates a AppDate instance from a UTC time string.
|
|
123
|
+
*
|
|
124
|
+
* @param time - A string representing a UTC time in "HH:mm:ssZ" format.
|
|
125
|
+
* @returns A new AppDate instance set to the given UTC time.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const time = AppDate.fromUtcTime("14:30:00+00:00");
|
|
129
|
+
* console.log(time.toLocalizedDateString()); // "04.01.2026"
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
static fromUtcTime(time: string): AppDate;
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
* @returns A new AppDate instance set to the minimum supported date (1900-01-01).
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* const minDate = AppDate.minDate();
|
|
139
|
+
* console.log(minDate.toLocalizedDateString()); // "01.01.1900"
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
static minDate(): AppDate;
|
|
143
|
+
/**
|
|
144
|
+
* Returns the maximum supported date (2200-12-31).
|
|
145
|
+
*
|
|
146
|
+
* @returns A new AppDate instance set to the maximum supported date.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* const maxDate = AppDate.maxDate();
|
|
150
|
+
* console.log(maxDate.toLocalizedDateString()); // "31.12.2200"
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
static maxDate(): AppDate;
|
|
154
|
+
add(value: number, unit?: ManipulateType): AppDate;
|
|
155
|
+
subtract(value: number, unit?: ManipulateType): AppDate;
|
|
156
|
+
startOf(unit: OpUnitType): AppDate;
|
|
157
|
+
endOf(unit: OpUnitType): AppDate;
|
|
158
|
+
tomorrow(): AppDate;
|
|
159
|
+
isValid(): boolean;
|
|
160
|
+
isBefore(other: AppDate, unit?: OpUnitType): boolean;
|
|
161
|
+
isSame(other: AppDate, unit?: OpUnitType): boolean;
|
|
162
|
+
/**
|
|
163
|
+
* returns true if date is current day
|
|
164
|
+
*/
|
|
165
|
+
isToday(): boolean;
|
|
166
|
+
isAfter(other: AppDate, unit?: OpUnitType): boolean;
|
|
167
|
+
isBetween(from?: AppDate, to?: AppDate, unit?: OpUnitType, inclusivity?: `${"(" | "["}${")" | "]"}`): boolean;
|
|
168
|
+
isFirstDayOfWeek(): boolean;
|
|
169
|
+
isWorkingDay(): boolean;
|
|
170
|
+
nextWorkingDay(): AppDate;
|
|
171
|
+
previousWorkingDay(): AppDate;
|
|
172
|
+
addWorkingDays(days: number): AppDate;
|
|
173
|
+
/*** Formatters ***/
|
|
174
|
+
/**
|
|
175
|
+
* Converts Date in following string format: HH:mm (20:10)
|
|
176
|
+
*/
|
|
177
|
+
toLocalTime(): string;
|
|
178
|
+
/**
|
|
179
|
+
* Returns time in UTC format: HH:mm:ssZ
|
|
180
|
+
*/
|
|
181
|
+
toUtcTime(): string;
|
|
182
|
+
/**
|
|
183
|
+
* Converts the current date to a string: YYYY-MM-DD
|
|
184
|
+
*
|
|
185
|
+
* @returns A string representing the current date in "YYYY-MM-DD" format.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* const date = AppDate.now();
|
|
189
|
+
* console.log(date.toDateString()); // "2026-01-04"
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
toDateString(): DateString;
|
|
193
|
+
/**
|
|
194
|
+
* Locale friendly format:
|
|
195
|
+
* ```
|
|
196
|
+
* de, ch = 20.10.1985
|
|
197
|
+
* us = 10/20/1985
|
|
198
|
+
* {includeDayOfWeek: true}
|
|
199
|
+
* de, ch = Son, 20.10.1985
|
|
200
|
+
* us = Sun, 10/20/1985
|
|
201
|
+
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
toLocalizedDateString({ includeDayOfWeek, }?: LocalizedFormatOptions): string;
|
|
205
|
+
toUtcDateString(): DateString;
|
|
206
|
+
toUtcString(): string;
|
|
207
|
+
/**
|
|
208
|
+
* Converts the current date to a epoch seconds (unix timestamp).
|
|
209
|
+
*
|
|
210
|
+
* @returns The epoch seconds.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* const date = AppDate.now();
|
|
214
|
+
* console.log(date.toEpochSeconds()); // 1714732800
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
toEpochSeconds(): number;
|
|
218
|
+
/**
|
|
219
|
+
* Converts the current date to a epoch milliseconds (timestamp in milliseconds).
|
|
220
|
+
*
|
|
221
|
+
* @returns The epoch milliseconds.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* const date = AppDate.now();
|
|
225
|
+
* console.log(date.toEpochMillis()); // 1714732800000
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
toEpochMillis(): number;
|
|
229
|
+
/**
|
|
230
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
231
|
+
*
|
|
232
|
+
* To escape characters, wrap them in square brackets (e.g. [MM]).
|
|
233
|
+
*
|
|
234
|
+
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
235
|
+
*
|
|
236
|
+
*/
|
|
237
|
+
format(template?: FormatTemplate): string;
|
|
238
|
+
/**
|
|
239
|
+
*
|
|
240
|
+
* @param options - Optional settings for short formatting.
|
|
241
|
+
* @returns The short formatted date string.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* const date = AppDate.now();
|
|
245
|
+
* console.log(date.formatShort({ includeDayOfWeek: true })); // "Su, 04.01."
|
|
246
|
+
* console.log(date.formatShort({ includeDayOfWeek: false })); // "04.01."
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
250
|
+
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
251
|
+
}
|
|
252
|
+
interface LocalizedFormatOptions {
|
|
253
|
+
includeDayOfWeek?: boolean;
|
|
254
|
+
}
|
|
255
|
+
interface GetNDaysOptions {
|
|
256
|
+
startDate?: AppDate;
|
|
257
|
+
excludeStartDate?: boolean;
|
|
258
|
+
}
|
|
259
|
+
declare function getLocalizedDateString(date: string, options?: LocalizedFormatOptions): string;
|
|
260
|
+
declare function formatLocalTime(time: string): string;
|
|
261
|
+
/**
|
|
262
|
+
* returns true if given argument is in YYYY-MM-DD format
|
|
263
|
+
* otherwise false
|
|
264
|
+
*/
|
|
265
|
+
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
266
|
+
type FormatTemplate = "YY" | "YYYY" | "M" | "MM" | "MMM" | "MMMM" | "D" | "DD" | "d" | "dd" | "ddd" | "dddd" | "H" | "HH" | "h" | "hh" | "m" | "mm" | "s" | "ss" | "SSS" | "Z" | "ZZ" | "A" | "a" | "LT" | "LTS" | /* en: 10/10/2020, de: 10.10.2020 */ "L" | "LL" | "LLL" | "LLLL" | "l" | "ll" | "lll" | "llll" | (string & {});
|
|
267
|
+
|
|
268
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import r from"dayjs";import"dayjs/locale/de";import c from"dayjs/plugin/customParseFormat.js";import u from"dayjs/plugin/isBetween.js";import m from"dayjs/plugin/localizedFormat.js";import p from"dayjs/plugin/timezone.js";import l from"dayjs/plugin/utc.js";r.extend(l);r.extend(p);r.extend(c);r.extend(u);r.extend(m);async function w(a){switch(a){case"de":{let t=await import("dayjs/locale/de-ch");r.locale(t.default);break}case"fr":{let t=await import("dayjs/locale/fr-ch");r.locale(t.default);break}case"en":default:{let t=await import("dayjs/locale/en");r.locale(t.default);break}}}var n="Europe/Zurich";function Y(a){n=a}var d="HH:mm",D="HH:mm:ssZ",s=class a{timezone;dayjsDate;static INVALID_DATE=r("");constructor(t,e,{invalid:i}={invalid:!1}){if(this.timezone=t,i){this.dayjsDate=a.INVALID_DATE;return}try{if(typeof e=="string"&&!f(e))throw new Error("Invalid Date string, we expect YYYY-DD-MM");this.dayjsDate=r.tz(e,t)}catch{console.warn("Could not parse date:",e),this.dayjsDate=a.INVALID_DATE}}static invalid(){return new a(n,"",{invalid:!0})}static now(){return new a(n,r())}static fromDateString(t){return new a(n,t)}static fromEpochSeconds(t){let e=r.unix(t);return new a(n,e)}static fromEpochMillis(t){let e=r(t);return new a(n,e)}static fromLocalTime(t){try{let e=r.tz(t,d,n);return new a(n,e)}catch(e){return e instanceof Error&&console.warn(`fromLocalTime(): ${e.message}`),a.invalid()}}static fromUtcString(t){let e=r.utc(t);return new a(n,e)}static fromUtcTime(t){let e=r.utc(t,D);return new a(n,e)}static minDate(){return a.fromDateString("1900-01-01")}static maxDate(){return a.fromDateString("2200-12-31")}add(t,e){let i=this.dayjsDate.add(t,e);return new a(n,i)}subtract(t,e){let i=this.dayjsDate.subtract(t,e);return new a(n,i)}startOf(t){let e=this.dayjsDate.startOf(t);return new a(n,e)}endOf(t){let e=this.dayjsDate.endOf(t);return new a(n,e)}tomorrow(){return this.add(1,"day")}isValid(){return this.dayjsDate.isValid()}isBefore(t,e){return this.dayjsDate.isBefore(t.dayjsDate,e)}isSame(t,e){return this.dayjsDate.isSame(t.dayjsDate,e)}isToday(){return this.dayjsDate.endOf("d").isSame(r().endOf("d"))}isAfter(t,e){return this.dayjsDate.isAfter(t.dayjsDate,e)}isBetween(t=a.minDate(),e=a.maxDate(),i,o){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,i,o??"[)")}isFirstDayOfWeek(){return this.dayjsDate.day()===1}isWorkingDay(){return y.includes(this.dayjsDate.day())}nextWorkingDay(){let t=this.add(1,"day");return t.isWorkingDay()?t:t.nextWorkingDay()}previousWorkingDay(){let t=this.subtract(1,"day");return t.isWorkingDay()?t:t.previousWorkingDay()}addWorkingDays(t){return t<=0||!Number.isInteger(t)?this:this.nextWorkingDay().addWorkingDays(t-1)}toLocalTime(){return this.format(d)}toUtcTime(){return this.dayjsDate.utc().format(D)}toDateString(){return this.format("YYYY-MM-DD")}toLocalizedDateString({includeDayOfWeek:t=!1}={}){let e=this.dayjsDate.format("L");return t?this.dayjsDate.format("dd, ")+e:e}toUtcDateString(){return this.dayjsDate.utc().format("YYYY-MM-DD")}toUtcString(){return this.dayjsDate.utc().format()}toEpochSeconds(){return this.dayjsDate.unix()}toEpochMillis(){return this.dayjsDate.valueOf()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){return t?this.format("dd, DD.MM."):this.format("DD.MM.")}formatDateTime({includeDayOfWeek:t=!0}={}){let e=this.toLocalizedDateString({includeDayOfWeek:t}),i=this.toLocalTime();return`${e}, ${i}`}},y=[1,2,3,4,5];function O(a,t){return s.fromDateString(a).toLocalizedDateString(t)}function A(a){return s.fromLocalTime(a).toLocalTime()}function f(a){return a?r(a,"YYYY-MM-DD",!0).isValid():!1}export{s as AppDate,A as formatLocalTime,O as getLocalizedDateString,f as isDateString,w as setAppDateLanguage,Y as setTimezone};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.8",
|
|
3
3
|
"name": "@ma.vu/appdate",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "tsup",
|
|
24
24
|
"test": "bun test",
|
|
25
|
-
"docs": "typedoc src/index.ts"
|
|
25
|
+
"docs": "typedoc src/index.ts",
|
|
26
|
+
"bump": "npm version patch",
|
|
27
|
+
"prepublishOnly": "bun run build && bun run docs"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
30
|
"bun-types": "latest",
|