@ma.vu/appdate 0.0.1
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 +15 -0
- package/dist/appdate.cjs +2 -0
- package/dist/appdate.d.cts +161 -0
- package/dist/appdate.d.ts +161 -0
- package/dist/appdate.js +2 -0
- package/package.json +36 -0
package/README.md
ADDED
package/dist/appdate.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";var T=Object.create;var D=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var j=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 j(t))!S.call(a,o)&&o!==e&&D(a,o,{get:()=>t[o],enumerable:!(i=L(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 U={};w(U,{AppDate:()=>d,formatLocalTime:()=>k,getLocalizedDateString:()=>z,isDateString:()=>h,setAppDateLanguage:()=>O,setTimezone:()=>A});module.exports=Y(U);var r=s(require("dayjs"),1),F=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 m="HH:mm",u="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 fromLocalTime(t){try{let e=r.default.tz(t,m,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,u);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(m)}toUtcTime(){return this.dayjsDate.utc().format(u)}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()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){let e=this.format("DD.MM.");return t?this.format("dd, ")+e:e}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 k(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});
|
|
@@ -0,0 +1,161 @@
|
|
|
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 local time string.
|
|
72
|
+
*
|
|
73
|
+
* @param time - A string representing a local time in any valid (24h) time format.
|
|
74
|
+
* @returns A new AppDate instance set to the given time on the todays date.
|
|
75
|
+
*
|
|
76
|
+
* If the time string is invalid, it returns an invalid AppDate instance.
|
|
77
|
+
* The date part defaults to the current date in the local timezone.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* const date = AppDate.fromLocalTime("14:30"); Today's date, 14:30 or (02:30 PM)
|
|
81
|
+
*/
|
|
82
|
+
static fromLocalTime(time: string): AppDate;
|
|
83
|
+
static fromUtcString(date?: string): AppDate;
|
|
84
|
+
static fromUtcTime(time: string): AppDate;
|
|
85
|
+
static minDate(): AppDate;
|
|
86
|
+
static maxDate(): AppDate;
|
|
87
|
+
add(value: number, unit?: ManipulateType): AppDate;
|
|
88
|
+
subtract(value: number, unit?: ManipulateType): AppDate;
|
|
89
|
+
startOf(unit: OpUnitType): AppDate;
|
|
90
|
+
endOf(unit: OpUnitType): AppDate;
|
|
91
|
+
tomorrow(): AppDate;
|
|
92
|
+
isValid(): boolean;
|
|
93
|
+
isBefore(other: AppDate, unit?: OpUnitType): boolean;
|
|
94
|
+
isSame(other: AppDate, unit?: OpUnitType): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* returns true if date is current day
|
|
97
|
+
*/
|
|
98
|
+
isToday(): boolean;
|
|
99
|
+
isAfter(other: AppDate, unit?: OpUnitType): boolean;
|
|
100
|
+
isBetween(from?: AppDate, to?: AppDate, unit?: OpUnitType, inclusivity?: `${'(' | '['}${')' | ']'}`): boolean;
|
|
101
|
+
isFirstDayOfWeek(): boolean;
|
|
102
|
+
isWorkingDay(): boolean;
|
|
103
|
+
nextWorkingDay(): AppDate;
|
|
104
|
+
previousWorkingDay(): AppDate;
|
|
105
|
+
addWorkingDays(days: number): AppDate;
|
|
106
|
+
/*** Formatters ***/
|
|
107
|
+
/**
|
|
108
|
+
* Converts Date in following string format: HH:mm (20:10)
|
|
109
|
+
*/
|
|
110
|
+
toLocalTime(): string;
|
|
111
|
+
/**
|
|
112
|
+
* Returns time in UTC format: HH:mm:ssZ
|
|
113
|
+
*/
|
|
114
|
+
toUtcTime(): string;
|
|
115
|
+
/**
|
|
116
|
+
* Converts the current date to a string: YYYY-MM-DD
|
|
117
|
+
*/
|
|
118
|
+
toDateString(): DateString;
|
|
119
|
+
/**
|
|
120
|
+
* Locale friendly format:
|
|
121
|
+
* ```
|
|
122
|
+
* de, ch = 20.10.1985
|
|
123
|
+
* us = 10/20/1985
|
|
124
|
+
* {includeDayOfWeek: true}
|
|
125
|
+
* de, ch = Son, 20.10.1985
|
|
126
|
+
* us = Sun, 10/20/1985
|
|
127
|
+
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
toLocalizedDateString({ includeDayOfWeek, }?: LocalizedFormatOptions): string;
|
|
131
|
+
toUtcDateString(): DateString;
|
|
132
|
+
toUtcString(): string;
|
|
133
|
+
/**
|
|
134
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
135
|
+
*
|
|
136
|
+
* To escape characters, wrap them in square brackets (e.g. [MM]).
|
|
137
|
+
*
|
|
138
|
+
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
format(template?: FormatTemplate): string;
|
|
142
|
+
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
143
|
+
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
144
|
+
}
|
|
145
|
+
interface LocalizedFormatOptions {
|
|
146
|
+
includeDayOfWeek?: boolean;
|
|
147
|
+
}
|
|
148
|
+
interface GetNDaysOptions {
|
|
149
|
+
startDate?: AppDate;
|
|
150
|
+
excludeStartDate?: boolean;
|
|
151
|
+
}
|
|
152
|
+
declare function getLocalizedDateString(date: string, options?: LocalizedFormatOptions): string;
|
|
153
|
+
declare function formatLocalTime(time: string): string;
|
|
154
|
+
/**
|
|
155
|
+
* returns true if given argument is in YYYY-MM-DD format
|
|
156
|
+
* otherwise false
|
|
157
|
+
*/
|
|
158
|
+
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
159
|
+
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 & {});
|
|
160
|
+
|
|
161
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
|
@@ -0,0 +1,161 @@
|
|
|
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 local time string.
|
|
72
|
+
*
|
|
73
|
+
* @param time - A string representing a local time in any valid (24h) time format.
|
|
74
|
+
* @returns A new AppDate instance set to the given time on the todays date.
|
|
75
|
+
*
|
|
76
|
+
* If the time string is invalid, it returns an invalid AppDate instance.
|
|
77
|
+
* The date part defaults to the current date in the local timezone.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* const date = AppDate.fromLocalTime("14:30"); Today's date, 14:30 or (02:30 PM)
|
|
81
|
+
*/
|
|
82
|
+
static fromLocalTime(time: string): AppDate;
|
|
83
|
+
static fromUtcString(date?: string): AppDate;
|
|
84
|
+
static fromUtcTime(time: string): AppDate;
|
|
85
|
+
static minDate(): AppDate;
|
|
86
|
+
static maxDate(): AppDate;
|
|
87
|
+
add(value: number, unit?: ManipulateType): AppDate;
|
|
88
|
+
subtract(value: number, unit?: ManipulateType): AppDate;
|
|
89
|
+
startOf(unit: OpUnitType): AppDate;
|
|
90
|
+
endOf(unit: OpUnitType): AppDate;
|
|
91
|
+
tomorrow(): AppDate;
|
|
92
|
+
isValid(): boolean;
|
|
93
|
+
isBefore(other: AppDate, unit?: OpUnitType): boolean;
|
|
94
|
+
isSame(other: AppDate, unit?: OpUnitType): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* returns true if date is current day
|
|
97
|
+
*/
|
|
98
|
+
isToday(): boolean;
|
|
99
|
+
isAfter(other: AppDate, unit?: OpUnitType): boolean;
|
|
100
|
+
isBetween(from?: AppDate, to?: AppDate, unit?: OpUnitType, inclusivity?: `${'(' | '['}${')' | ']'}`): boolean;
|
|
101
|
+
isFirstDayOfWeek(): boolean;
|
|
102
|
+
isWorkingDay(): boolean;
|
|
103
|
+
nextWorkingDay(): AppDate;
|
|
104
|
+
previousWorkingDay(): AppDate;
|
|
105
|
+
addWorkingDays(days: number): AppDate;
|
|
106
|
+
/*** Formatters ***/
|
|
107
|
+
/**
|
|
108
|
+
* Converts Date in following string format: HH:mm (20:10)
|
|
109
|
+
*/
|
|
110
|
+
toLocalTime(): string;
|
|
111
|
+
/**
|
|
112
|
+
* Returns time in UTC format: HH:mm:ssZ
|
|
113
|
+
*/
|
|
114
|
+
toUtcTime(): string;
|
|
115
|
+
/**
|
|
116
|
+
* Converts the current date to a string: YYYY-MM-DD
|
|
117
|
+
*/
|
|
118
|
+
toDateString(): DateString;
|
|
119
|
+
/**
|
|
120
|
+
* Locale friendly format:
|
|
121
|
+
* ```
|
|
122
|
+
* de, ch = 20.10.1985
|
|
123
|
+
* us = 10/20/1985
|
|
124
|
+
* {includeDayOfWeek: true}
|
|
125
|
+
* de, ch = Son, 20.10.1985
|
|
126
|
+
* us = Sun, 10/20/1985
|
|
127
|
+
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
toLocalizedDateString({ includeDayOfWeek, }?: LocalizedFormatOptions): string;
|
|
131
|
+
toUtcDateString(): DateString;
|
|
132
|
+
toUtcString(): string;
|
|
133
|
+
/**
|
|
134
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
135
|
+
*
|
|
136
|
+
* To escape characters, wrap them in square brackets (e.g. [MM]).
|
|
137
|
+
*
|
|
138
|
+
* @see {@link https://day.js.org/docs/en/display/format|Day.js format documentation}
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
format(template?: FormatTemplate): string;
|
|
142
|
+
formatShort({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
143
|
+
formatDateTime({ includeDayOfWeek }?: LocalizedFormatOptions): string;
|
|
144
|
+
}
|
|
145
|
+
interface LocalizedFormatOptions {
|
|
146
|
+
includeDayOfWeek?: boolean;
|
|
147
|
+
}
|
|
148
|
+
interface GetNDaysOptions {
|
|
149
|
+
startDate?: AppDate;
|
|
150
|
+
excludeStartDate?: boolean;
|
|
151
|
+
}
|
|
152
|
+
declare function getLocalizedDateString(date: string, options?: LocalizedFormatOptions): string;
|
|
153
|
+
declare function formatLocalTime(time: string): string;
|
|
154
|
+
/**
|
|
155
|
+
* returns true if given argument is in YYYY-MM-DD format
|
|
156
|
+
* otherwise false
|
|
157
|
+
*/
|
|
158
|
+
declare function isDateString(date: string | undefined | null | Dayjs): date is DateString;
|
|
159
|
+
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 & {});
|
|
160
|
+
|
|
161
|
+
export { AppDate, type GetNDaysOptions, type LocalizedFormatOptions, formatLocalTime, getLocalizedDateString, isDateString, setAppDateLanguage, setTimezone };
|
package/dist/appdate.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 m from"dayjs/plugin/isBetween.js";import u 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(m);r.extend(u);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 i="Europe/Zurich";function Y(a){i=a}var d="HH:mm",D="HH:mm:ssZ",s=class a{timezone;dayjsDate;static INVALID_DATE=r("");constructor(t,e,{invalid:n}={invalid:!1}){if(this.timezone=t,n){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(i,"",{invalid:!0})}static now(){return new a(i,r())}static fromDateString(t){return new a(i,t)}static fromLocalTime(t){try{let e=r.tz(t,d,i);return new a(i,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(i,e)}static fromUtcTime(t){let e=r.utc(t,D);return new a(i,e)}static minDate(){return a.fromDateString("1900-01-01")}static maxDate(){return a.fromDateString("2200-12-31")}add(t,e){let n=this.dayjsDate.add(t,e);return new a(i,n)}subtract(t,e){let n=this.dayjsDate.subtract(t,e);return new a(i,n)}startOf(t){let e=this.dayjsDate.startOf(t);return new a(i,e)}endOf(t){let e=this.dayjsDate.endOf(t);return new a(i,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(),n,o){return this.dayjsDate.isBetween(t.dayjsDate,e.dayjsDate,n,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()}format(t="YYYY-MM-DDTHH:mm:ssZ[Z]"){return this.dayjsDate.format(t)}formatShort({includeDayOfWeek:t=!0}={}){let e=this.format("DD.MM.");return t?this.format("dd, ")+e:e}formatDateTime({includeDayOfWeek:t=!0}={}){let e=this.toLocalizedDateString({includeDayOfWeek:t}),n=this.toLocalTime();return`${e}, ${n}`}},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
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.0.1",
|
|
3
|
+
"name": "@ma.vu/appdate",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"module": "dist/index.cjs",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.cjs",
|
|
15
|
+
"require": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [ "dist" ],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"test": "bun test"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"bun-types": "latest",
|
|
26
|
+
"tsup": "~8.2.4",
|
|
27
|
+
"vite": "~5.3.5",
|
|
28
|
+
"vitest": "~2.0.5"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"typescript": "^5.0.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"dayjs": "~1.11.12"
|
|
35
|
+
}
|
|
36
|
+
}
|