@connect-soft/date-helper 0.0.9 → 0.0.10

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## v0.0.10
4
+ - Extended `add()` method to support all time units (years, months, weeks, days, hours, minutes, seconds, milliseconds)
5
+ - New methods: `addYears()`, `addMonths()`, `addWeeks()`, `addMinutes()`, `addSeconds()`, `addMilliseconds()`
6
+ - `isAfter()` now returns `boolean` instead of `0` when dealing with null values
3
7
  ## v0.0.9
4
8
  - added support: changing of locale
5
9
  ## v0.0.8
package/README.md CHANGED
@@ -1 +1,221 @@
1
1
  # Date Helper
2
+
3
+ A TypeScript date manipulation library built on top of Day.js, providing a fluent API for working with dates, times, and locales.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @connect-soft/date-helper dayjs
9
+ ```
10
+
11
+ ```bash
12
+ pnpm add @connect-soft/date-helper dayjs
13
+ ```
14
+
15
+ ```bash
16
+ yarn add @connect-soft/date-helper dayjs
17
+ ```
18
+
19
+ **Note:** `dayjs` is a peer dependency and must be installed separately.
20
+
21
+ ## Features
22
+
23
+ - Fluent API for date manipulation
24
+ - Immutable or mutable operations (configurable)
25
+ - Timezone support
26
+ - Locale-aware formatting
27
+ - Type-safe with TypeScript
28
+ - Lightweight wrapper around Day.js
29
+ - Support for both ESM and CommonJS
30
+
31
+ ## Basic Usage
32
+
33
+ ```typescript
34
+ import { DateHelper } from '@connect-soft/date-helper';
35
+
36
+ // Create from current time
37
+ const now = DateHelper.now();
38
+
39
+ // Create from Date object
40
+ const date = DateHelper.from(new Date());
41
+
42
+ // Create from string
43
+ const parsed = DateHelper.fromDateTimeString('2024-10-02T08:11:00Z');
44
+
45
+ // Create from null (safe handling)
46
+ const nullable = DateHelper.from(null);
47
+ ```
48
+
49
+ ## API Reference
50
+
51
+ ### Static Methods
52
+
53
+ #### `DateHelper.now()`
54
+ Creates a DateHelper instance with the current date and time.
55
+
56
+ ```typescript
57
+ const now = DateHelper.now();
58
+ ```
59
+
60
+ #### `DateHelper.from(source)`
61
+ Creates a DateHelper instance from a Date, Dayjs object, or null.
62
+
63
+ ```typescript
64
+ const date = DateHelper.from(new Date());
65
+ const fromDayjs = DateHelper.from(dayjs());
66
+ const fromNull = DateHelper.from(null); // Returns DateHelper with null instance
67
+ ```
68
+
69
+ #### `DateHelper.fromDateTimeString(source)`
70
+ Creates a DateHelper instance from a date-time string.
71
+
72
+ ```typescript
73
+ const date = DateHelper.fromDateTimeString('2024-10-02T08:11:00Z');
74
+ ```
75
+
76
+ #### `DateHelper.addHours(source, hours)`
77
+ Static utility to add hours to a date.
78
+
79
+ ```typescript
80
+ const result = DateHelper.addHours(new Date(), 2);
81
+ ```
82
+
83
+ ### Instance Methods
84
+
85
+ #### Date Manipulation
86
+
87
+ ```typescript
88
+ // Add days (immutable by default)
89
+ const tomorrow = DateHelper.now().addDays(1, false);
90
+
91
+ // Add hours (mutable if update=true)
92
+ const later = DateHelper.now().addHours(2, true);
93
+
94
+ // Get start of day
95
+ const startOfDay = DateHelper.now().getStartOfDay();
96
+
97
+ // Get end of day
98
+ const endOfDay = DateHelper.now().getEndOfDay();
99
+ ```
100
+
101
+ #### Comparison
102
+
103
+ ```typescript
104
+ const date1 = DateHelper.now();
105
+ const date2 = DateHelper.now().addDays(1);
106
+
107
+ // Check if after
108
+ if (date2.isAfter(date1)) {
109
+ console.log('date2 is after date1');
110
+ }
111
+
112
+ // Check if equal
113
+ if (date1.isEqual(date1, 'day')) {
114
+ console.log('Same day');
115
+ }
116
+
117
+ // Calculate difference
118
+ const hoursDiff = date2.diff(date1, 'hours'); // Returns number
119
+ ```
120
+
121
+ #### Formatting
122
+
123
+ ```typescript
124
+ const date = DateHelper.now();
125
+
126
+ // Custom format using Day.js format
127
+ const formatted = date.format('YYYY-MM-DD HH:mm:ss');
128
+
129
+ // Locale-aware date formatting
130
+ const localDate = date.formatLocalDate('en-US'); // "1/15/2024"
131
+ const czechDate = date.formatLocalDate('cs'); // "15. 1. 2024"
132
+
133
+ // Locale-aware time formatting
134
+ const time = date.formatLocalTime('en-US'); // "2:30 PM"
135
+
136
+ // Locale-aware date-time formatting
137
+ const dateTime = date.formatLocalDateTime('cs'); // "15. 1. 2024 14:30"
138
+
139
+ // Custom locale options
140
+ const customFormat = date.formatLocalDate('en-US', {
141
+ weekday: 'long',
142
+ year: 'numeric',
143
+ month: 'long',
144
+ day: 'numeric'
145
+ }); // "Monday, January 15, 2024"
146
+ ```
147
+
148
+ #### Locale Management
149
+
150
+ ```typescript
151
+ // Get current locale
152
+ const locale = DateHelper.now().getLocale(); // "en"
153
+
154
+ // Set locale (requires importing locale first)
155
+ import 'dayjs/locale/cs';
156
+ const czechDate = DateHelper.now().setLocale('cs');
157
+ ```
158
+
159
+ #### Conversion
160
+
161
+ ```typescript
162
+ // Convert to Date object
163
+ const dateObj = DateHelper.now().toDate();
164
+
165
+ // Get underlying Day.js instance
166
+ const dayjsObj = DateHelper.now().getObj();
167
+ ```
168
+
169
+ ## Advanced Examples
170
+
171
+ ### Working with Timezones
172
+
173
+ ```typescript
174
+ import { DateHelper } from '@connect-soft/date-helper';
175
+
176
+ // The library includes timezone plugin by default
177
+ const date = DateHelper.fromDateTimeString('2024-01-15T14:30:00Z');
178
+ ```
179
+
180
+ ### Null-Safe Operations
181
+
182
+ ```typescript
183
+ // All methods handle null gracefully
184
+ const nullable = DateHelper.from(null);
185
+ nullable.addDays(5).toDate(); // Returns null
186
+ nullable.format('YYYY-MM-DD', 'N/A'); // Returns 'N/A'
187
+ nullable.formatLocalDate('en-US'); // Returns ""
188
+ ```
189
+
190
+ ### Chaining Operations
191
+
192
+ ```typescript
193
+ const result = DateHelper.now()
194
+ .addDays(7)
195
+ .getStartOfDay()
196
+ .addHours(9)
197
+ .format('YYYY-MM-DD HH:mm');
198
+ ```
199
+
200
+ ## TypeScript Support
201
+
202
+ This library is written in TypeScript and includes full type definitions.
203
+
204
+ ```typescript
205
+ import { DateHelper, IDateTimeType } from '@connect-soft/date-helper';
206
+
207
+ // IDateTimeType is an alias for Day.js Dayjs type
208
+ const dayjs: IDateTimeType = DateHelper.now().getObj();
209
+ ```
210
+
211
+ ## License
212
+
213
+ MIT
214
+
215
+ ## Contributing
216
+
217
+ Contributions are welcome! Please feel free to submit a Pull Request.
218
+
219
+ ## Repository
220
+
221
+ [GitLab Repository](https://gitlab.com/connect-soft/date-helper)
package/dist/index.d.mts CHANGED
@@ -2,17 +2,17 @@ import dayjsInstance, { Dayjs } from 'dayjs';
2
2
 
3
3
  type IDateTimeType = Dayjs;
4
4
  type FormaterOptions = {
5
- localeMatcher?: "best fit" | "lookup";
6
- weekday?: "long" | "short" | "narrow";
7
- era?: "long" | "short" | "narrow";
8
- year?: "numeric" | "2-digit";
9
- month?: "numeric" | "2-digit" | "long" | "short" | "narrow";
10
- day?: "numeric" | "2-digit";
11
- hour?: "numeric" | "2-digit";
12
- minute?: "numeric" | "2-digit";
13
- second?: "numeric" | "2-digit";
14
- timeZoneName?: "short" | "long" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric";
15
- formatMatcher?: "best fit" | "basic";
5
+ localeMatcher?: 'best fit' | 'lookup';
6
+ weekday?: 'long' | 'short' | 'narrow';
7
+ era?: 'long' | 'short' | 'narrow';
8
+ year?: 'numeric' | '2-digit';
9
+ month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
10
+ day?: 'numeric' | '2-digit';
11
+ hour?: 'numeric' | '2-digit';
12
+ minute?: 'numeric' | '2-digit';
13
+ second?: 'numeric' | '2-digit';
14
+ timeZoneName?: 'short' | 'long' | 'shortOffset' | 'longOffset' | 'shortGeneric' | 'longGeneric';
15
+ formatMatcher?: 'best fit' | 'basic';
16
16
  hour12?: boolean;
17
17
  timeZone?: string;
18
18
  };
@@ -23,10 +23,16 @@ declare class DateHelper {
23
23
  static from(source: Date | IDateTimeType | null | undefined): DateHelper;
24
24
  static now(): DateHelper;
25
25
  static addHours(source: IDateTimeType | Date | null, hours: number): dayjsInstance.Dayjs | Date | null;
26
+ addYears(years: number, update?: boolean): DateHelper;
27
+ addMonths(months: number, update?: boolean): DateHelper;
28
+ addWeeks(weeks: number, update?: boolean): DateHelper;
26
29
  addDays(days: number, update?: boolean): DateHelper;
27
30
  addHours(hours: number, update?: boolean): DateHelper;
31
+ addMinutes(minutes: number, update?: boolean): DateHelper;
32
+ addSeconds(seconds: number, update?: boolean): DateHelper;
33
+ addMilliseconds(milliseconds: number, update?: boolean): DateHelper;
28
34
  getObj(): dayjsInstance.Dayjs | null;
29
- isAfter(before: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean | 0;
35
+ isAfter(before: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean;
30
36
  isEqual(equal: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean;
31
37
  toDate(): Date | null;
32
38
  getStartOfDay(update?: boolean): DateHelper;
@@ -39,7 +45,7 @@ declare class DateHelper {
39
45
  formatLocalDateTime(locale?: string, options?: FormaterOptions): string;
40
46
  getLocale(): string | null;
41
47
  /**
42
- * Do you heave imported file with locale before setting of locale?
48
+ * Have you imported the locale file before setting the locale?
43
49
  * @param locale
44
50
  */
45
51
  setLocale(locale: string): this;
package/dist/index.d.ts CHANGED
@@ -1 +1,54 @@
1
- export * from './lib/DateHelper';
1
+ import dayjsInstance, { Dayjs } from 'dayjs';
2
+
3
+ type IDateTimeType = Dayjs;
4
+ type FormaterOptions = {
5
+ localeMatcher?: 'best fit' | 'lookup';
6
+ weekday?: 'long' | 'short' | 'narrow';
7
+ era?: 'long' | 'short' | 'narrow';
8
+ year?: 'numeric' | '2-digit';
9
+ month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';
10
+ day?: 'numeric' | '2-digit';
11
+ hour?: 'numeric' | '2-digit';
12
+ minute?: 'numeric' | '2-digit';
13
+ second?: 'numeric' | '2-digit';
14
+ timeZoneName?: 'short' | 'long' | 'shortOffset' | 'longOffset' | 'shortGeneric' | 'longGeneric';
15
+ formatMatcher?: 'best fit' | 'basic';
16
+ hour12?: boolean;
17
+ timeZone?: string;
18
+ };
19
+ declare class DateHelper {
20
+ private instance;
21
+ private constructor();
22
+ static fromDateTimeString(source: string): DateHelper;
23
+ static from(source: Date | IDateTimeType | null | undefined): DateHelper;
24
+ static now(): DateHelper;
25
+ static addHours(source: IDateTimeType | Date | null, hours: number): dayjsInstance.Dayjs | Date | null;
26
+ addYears(years: number, update?: boolean): DateHelper;
27
+ addMonths(months: number, update?: boolean): DateHelper;
28
+ addWeeks(weeks: number, update?: boolean): DateHelper;
29
+ addDays(days: number, update?: boolean): DateHelper;
30
+ addHours(hours: number, update?: boolean): DateHelper;
31
+ addMinutes(minutes: number, update?: boolean): DateHelper;
32
+ addSeconds(seconds: number, update?: boolean): DateHelper;
33
+ addMilliseconds(milliseconds: number, update?: boolean): DateHelper;
34
+ getObj(): dayjsInstance.Dayjs | null;
35
+ isAfter(before: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean;
36
+ isEqual(equal: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean;
37
+ toDate(): Date | null;
38
+ getStartOfDay(update?: boolean): DateHelper;
39
+ getEndOfDay(update?: boolean): DateHelper;
40
+ diff(source: IDateTimeType | Date | null | DateHelper, unit: 'days' | 'hours' | 'minutes', float?: boolean): number;
41
+ private add;
42
+ format(format: string, defaultValue?: string | null): string | null;
43
+ formatLocalDate(locale?: string, options?: FormaterOptions): string;
44
+ formatLocalTime(locale?: string, options?: FormaterOptions): string;
45
+ formatLocalDateTime(locale?: string, options?: FormaterOptions): string;
46
+ getLocale(): string | null;
47
+ /**
48
+ * Have you imported the locale file before setting the locale?
49
+ * @param locale
50
+ */
51
+ setLocale(locale: string): this;
52
+ }
53
+
54
+ export { DateHelper, type IDateTimeType };
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var y=Object.create;var u=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames,c=Object.getOwnPropertySymbols,w=Object.getPrototypeOf,d=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var m=(n,t,e)=>t in n?u(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,s=(n,t)=>{for(var e in t||(t={}))d.call(t,e)&&m(n,e,t[e]);if(c)for(var e of c(t))j.call(t,e)&&m(n,e,t[e]);return n};var I=(n,t)=>{for(var e in t)u(n,e,{get:t[e],enumerable:!0})},f=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of T(t))!d.call(n,r)&&r!==e&&u(n,r,{get:()=>t[r],enumerable:!(i=O(t,r))||i.enumerable});return n};var l=(n,t,e)=>(e=n!=null?y(w(n)):{},f(t||!n||!n.__esModule?u(e,"default",{value:n,enumerable:!0}):e,n)),F=n=>f(u({},"__esModule",{value:!0}),n);var H={};I(H,{DateHelper:()=>o});module.exports=F(H);var a=l(require("dayjs")),h=l(require("dayjs/plugin/isBetween")),p=l(require("dayjs/plugin/timezone")),b=l(require("dayjs/plugin/utc"));a.default.extend(h.default);a.default.extend(b.default);a.default.extend(p.default);var g={day:"numeric",month:"numeric",year:"numeric"},D={hour:"numeric",minute:"numeric"},x=s(s({},g),D),o=class n{constructor(t){this.instance=t}static fromDateTimeString(t){return new n((0,a.default)(t))}static from(t){return t?t instanceof Date?new n((0,a.default)(t)):new n((0,a.default)(t)):new n(null)}static now(){return new n((0,a.default)())}static addHours(t,e){return t===null||e===0?t:n.from(t).addHours(e).getObj()}addDays(t,e=!0){return this.add("days",t,e)}addHours(t,e=!0){return this.add("hours",t,e)}getObj(){return this.instance}isAfter(t,e="milliseconds"){return this.instance===null||t===null?0:this.instance.isAfter(t instanceof n?t.getObj():n.from(t).getObj(),e)}isEqual(t,e="milliseconds"){return this.instance===null&&t===null?!0:this.instance===null||t===null?!1:this.instance.isSame(t instanceof n?t.getObj():n.from(t).getObj(),e)}toDate(){var t,e;return(e=(t=this.instance)==null?void 0:t.toDate())!=null?e:null}getStartOfDay(t=!0){if(this.instance!==null){let e=this.instance.startOf("day");return t?(this.instance=e,this):n.from(e)}else return this}getEndOfDay(t=!0){if(this.instance!==null){let e=this.instance.endOf("day");return t?(this.instance=e,this):n.from(e)}else return this}diff(t,e,i=!0){return this.instance===null||t===null?0:this.instance.diff(t instanceof n?t.getObj():n.from(t).getObj(),e,i)}add(t,e,i=!0){if(this.instance!==null){let r=this.instance.add(e,t);return i?(this.instance=r,this):n.from(r)}else return this}format(t,e=null){var i,r;return(r=(i=this.instance)==null?void 0:i.format(t))!=null?r:e}formatLocalDate(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},g),e)).format(i)}formatLocalTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},D),e)).format(i)}formatLocalDateTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},x),e)).format(i)}getLocale(){var t,e;return(e=(t=this.instance)==null?void 0:t.locale())!=null?e:null}setLocale(t){return this.instance!==null&&(this.instance=this.instance.locale(t)),this}};0&&(module.exports={DateHelper});
1
+ "use strict";var D=Object.create;var u=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames,c=Object.getOwnPropertySymbols,w=Object.getPrototypeOf,d=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var m=(n,t,e)=>t in n?u(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,s=(n,t)=>{for(var e in t||(t={}))d.call(t,e)&&m(n,e,t[e]);if(c)for(var e of c(t))j.call(t,e)&&m(n,e,t[e]);return n};var I=(n,t)=>{for(var e in t)u(n,e,{get:t[e],enumerable:!0})},h=(n,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of T(t))!d.call(n,r)&&r!==e&&u(n,r,{get:()=>t[r],enumerable:!(i=O(t,r))||i.enumerable});return n};var l=(n,t,e)=>(e=n!=null?D(w(n)):{},h(t||!n||!n.__esModule?u(e,"default",{value:n,enumerable:!0}):e,n)),F=n=>h(u({},"__esModule",{value:!0}),n);var x={};I(x,{DateHelper:()=>o});module.exports=F(x);var a=l(require("dayjs")),f=l(require("dayjs/plugin/isBetween")),p=l(require("dayjs/plugin/timezone")),b=l(require("dayjs/plugin/utc"));a.default.extend(f.default);a.default.extend(b.default);a.default.extend(p.default);var g={day:"numeric",month:"numeric",year:"numeric"},y={hour:"numeric",minute:"numeric"},k=s(s({},g),y),o=class n{constructor(t){this.instance=t}static fromDateTimeString(t){return new n((0,a.default)(t))}static from(t){return t?t instanceof Date?new n((0,a.default)(t)):new n((0,a.default)(t)):new n(null)}static now(){return new n((0,a.default)())}static addHours(t,e){return t===null||e===0?t:n.from(t).addHours(e).getObj()}addYears(t,e=!0){return this.add("years",t,e)}addMonths(t,e=!0){return this.add("months",t,e)}addWeeks(t,e=!0){return this.add("weeks",t,e)}addDays(t,e=!0){return this.add("days",t,e)}addHours(t,e=!0){return this.add("hours",t,e)}addMinutes(t,e=!0){return this.add("minutes",t,e)}addSeconds(t,e=!0){return this.add("seconds",t,e)}addMilliseconds(t,e=!0){return this.add("milliseconds",t,e)}getObj(){return this.instance}isAfter(t,e="milliseconds"){return this.instance===null||t===null?!1:this.instance.isAfter(t instanceof n?t.getObj():n.from(t).getObj(),e)}isEqual(t,e="milliseconds"){return this.instance===null&&t===null?!0:this.instance===null||t===null?!1:this.instance.isSame(t instanceof n?t.getObj():n.from(t).getObj(),e)}toDate(){var t,e;return(e=(t=this.instance)==null?void 0:t.toDate())!=null?e:null}getStartOfDay(t=!0){if(this.instance!==null){let e=this.instance.startOf("day");return t?(this.instance=e,this):n.from(e)}else return this}getEndOfDay(t=!0){if(this.instance!==null){let e=this.instance.endOf("day");return t?(this.instance=e,this):n.from(e)}else return this}diff(t,e,i=!0){return this.instance===null||t===null?0:this.instance.diff(t instanceof n?t.getObj():n.from(t).getObj(),e,i)}add(t,e,i=!0){if(this.instance!==null){let r=this.instance.add(e,t);return i?(this.instance=r,this):n.from(r)}else return this}format(t,e=null){var i,r;return(r=(i=this.instance)==null?void 0:i.format(t))!=null?r:e}formatLocalDate(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},g),e)).format(i)}formatLocalTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},y),e)).format(i)}formatLocalDateTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,s(s({},k),e)).format(i)}getLocale(){var t,e;return(e=(t=this.instance)==null?void 0:t.locale())!=null?e:null}setLocale(t){return this.instance!==null&&(this.instance=this.instance.locale(t)),this}};0&&(module.exports={DateHelper});
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/lib/DateHelper.ts"],"sourcesContent":["export * from './lib/DateHelper';\n","import dayjsInstance, { Dayjs } from 'dayjs';\nimport isBetween from 'dayjs/plugin/isBetween'\nimport timezone from 'dayjs/plugin/timezone'\nimport utc from 'dayjs/plugin/utc'\n\ndayjsInstance.extend(isBetween);\ndayjsInstance.extend(utc);\ndayjsInstance.extend(timezone);\n\nexport type IDateTimeType = Dayjs;\n\ntype FormaterOptions = {\n localeMatcher?: \"best fit\" | \"lookup\";\n weekday?: \"long\" | \"short\" | \"narrow\";\n era?: \"long\" | \"short\" | \"narrow\";\n year?: \"numeric\" | \"2-digit\";\n month?: \"numeric\" | \"2-digit\" | \"long\" | \"short\" | \"narrow\";\n day?: \"numeric\" | \"2-digit\";\n hour?: \"numeric\" | \"2-digit\";\n minute?: \"numeric\" | \"2-digit\";\n second?: \"numeric\" | \"2-digit\";\n timeZoneName?: \"short\" | \"long\" | \"shortOffset\" | \"longOffset\" | \"shortGeneric\" | \"longGeneric\";\n formatMatcher?: \"best fit\" | \"basic\";\n hour12?: boolean;\n timeZone?: string;\n}\n\nconst dateOptions: FormaterOptions = {\n day: \"numeric\",month: \"numeric\", year: \"numeric\"\n}\nconst timeOptions: FormaterOptions = {\n hour: \"numeric\",minute: \"numeric\"\n}\nconst dateTimeOptions: FormaterOptions = {\n ...dateOptions,\n ...timeOptions\n}\n\nexport class DateHelper {\n private constructor(private instance: IDateTimeType | null) {}\n\n public static fromDateTimeString(source: string) {\n return new DateHelper(dayjsInstance(source));\n }\n\n public static from(source: Date | IDateTimeType | null | undefined) {\n if (!source) {\n return new DateHelper(null);\n }\n if (source instanceof Date) {\n return new DateHelper(dayjsInstance(source));\n }\n\n return new DateHelper(dayjsInstance(source));\n }\n\n public static now() {\n return new DateHelper(dayjsInstance());\n }\n\n public static addHours(source: IDateTimeType | Date | null, hours: number) {\n if (source === null || hours === 0) {\n return source;\n }\n\n return DateHelper.from(source).addHours(hours).getObj();\n }\n\n public addDays(days: number, update = true) {\n return this.add('days', days, update);\n }\n\n public addHours(hours: number, update = true) {\n return this.add('hours', hours, update);\n }\n\n public getObj() {\n return this.instance;\n }\n\n public isAfter(\n before: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null || before === null) {\n return 0;\n }\n return this.instance.isAfter(\n before instanceof DateHelper ? before.getObj() : DateHelper.from(before).getObj(),\n unit\n );\n }\n\n public isEqual(\n equal: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null && equal === null) {\n return true;\n } else if (this.instance === null || equal === null) {\n return false;\n }\n\n return this.instance.isSame(equal instanceof DateHelper ? equal.getObj() : DateHelper.from(equal).getObj(), unit);\n }\n\n public toDate() {\n return this.instance?.toDate() ?? null;\n }\n\n public getStartOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.startOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public getEndOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.endOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public diff(source: IDateTimeType | Date | null | DateHelper, unit: 'days' | 'hours' | 'minutes', float = true) {\n if (this.instance === null || source === null) {\n return 0;\n }\n return this.instance.diff(\n source instanceof DateHelper ? source.getObj() : DateHelper.from(source).getObj(),\n unit,\n float\n );\n }\n\n private add(type: 'days' | 'hours', value: number, update = true) {\n if (this.instance !== null) {\n const updated = this.instance.add(value, type);\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public format(format: string, defaultValue:string |null= null) {\n return this.instance?.format(format) ?? defaultValue\n }\n\n public formatLocalDate(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...dateOptions, ...options}).format(date)\n }\n\n public formatLocalTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...timeOptions, ...options}).format(date)\n }\n\n public formatLocalDateTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...dateTimeOptions, ...options}).format(date)\n }\n\n public getLocale() {\n return this.instance?.locale() ?? null;\n }\n\n /**\n * Do you heave imported file with locale before setting of locale?\n * @param locale\n */\n public setLocale(locale: string) {\n if (this.instance !== null) {\n this.instance = this.instance.locale(locale);\n }\n\n return this;\n }\n}\n"],"mappings":"y0BAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAqC,oBACrCC,EAAsB,qCACtBC,EAAqB,oCACrBC,EAAgB,+BAEhB,EAAAC,QAAc,OAAO,EAAAC,OAAS,EAC9B,EAAAD,QAAc,OAAO,EAAAE,OAAG,EACxB,EAAAF,QAAc,OAAO,EAAAG,OAAQ,EAoB7B,IAAMC,EAA+B,CACnC,IAAK,UAAU,MAAO,UAAW,KAAM,SACzC,EACMC,EAA+B,CACnC,KAAM,UAAU,OAAQ,SAC1B,EACMC,EAAmCC,IAAA,GACpCH,GACAC,GAGQG,EAAN,MAAMC,CAAW,CACd,YAAoBC,EAAgC,CAAhC,cAAAA,CAAiC,CAE7D,OAAc,mBAAmBC,EAAgB,CAC/C,OAAO,IAAIF,KAAW,EAAAT,SAAcW,CAAM,CAAC,CAC7C,CAEA,OAAc,KAAKA,EAAiD,CAClE,OAAKA,EAGDA,aAAkB,KACb,IAAIF,KAAW,EAAAT,SAAcW,CAAM,CAAC,EAGtC,IAAIF,KAAW,EAAAT,SAAcW,CAAM,CAAC,EANlC,IAAIF,EAAW,IAAI,CAO9B,CAEA,OAAc,KAAM,CAClB,OAAO,IAAIA,KAAW,EAAAT,SAAc,CAAC,CACvC,CAEA,OAAc,SAASW,EAAqCC,EAAe,CACzE,OAAID,IAAW,MAAQC,IAAU,EACxBD,EAGFF,EAAW,KAAKE,CAAM,EAAE,SAASC,CAAK,EAAE,OAAO,CACxD,CAEO,QAAQC,EAAcC,EAAS,GAAM,CAC1C,OAAO,KAAK,IAAI,OAAQD,EAA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C,EAAwBC,EAAeT,EAAS,GAAM,CAChE,GAAI,KAAK,WAAa,KAAM,CAC1B,IAAMM,EAAU,KAAK,SAAS,IAAIG,EAAOD,CAAI,EAE7C,OAAIR,GACF,KAAK,SAAWM,EAET,MAEAX,EAAW,KAAKW,CAAO,CAElC,KACE,QAAO,IAEX,CAEO,OAAOI,EAAgBC,EAA2B,KAAM,CA3LjE,IAAAP,EAAAC,EA4LI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,OAAOM,KAAtB,KAAAL,EAAiCM,CAC1C,CAEO,gBAAgBC,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQnB,IAAA,GAAIH,GAAgBuB,EAAQ,EAAE,OAAOC,CAAI,CAClF,CAEO,gBAAgBF,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQnB,IAAA,GAAIF,GAAgBsB,EAAQ,EAAE,OAAOC,CAAI,CAClF,CAEO,oBAAoBF,EAAiBC,EAA2B,CACrE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQnB,IAAA,GAAID,GAAoBqB,EAAQ,EAAE,OAAOC,CAAI,CACtF,CAEO,WAAY,CAvNrB,IAAAV,EAAAC,EAwNI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,WAAf,KAAAC,EAA2B,IACpC,CAMO,UAAUO,EAAgB,CAC/B,OAAI,KAAK,WAAa,OACpB,KAAK,SAAW,KAAK,SAAS,OAAOA,CAAM,GAGtC,IACT,CACF","names":["index_exports","__export","DateHelper","__toCommonJS","import_dayjs","import_isBetween","import_timezone","import_utc","dayjsInstance","isBetween","utc","timezone","dateOptions","timeOptions","dateTimeOptions","__spreadValues","DateHelper","_DateHelper","instance","source","hours","days","update","before","unit","equal","_a","_b","updated","float","type","value","format","defaultValue","locale","options","date"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/lib/DateHelper.ts"],"sourcesContent":["export * from './lib/DateHelper';\n","import dayjsInstance, { Dayjs } from 'dayjs';\nimport isBetween from 'dayjs/plugin/isBetween';\nimport timezone from 'dayjs/plugin/timezone';\nimport utc from 'dayjs/plugin/utc';\n\ndayjsInstance.extend(isBetween);\ndayjsInstance.extend(utc);\ndayjsInstance.extend(timezone);\n\nexport type IDateTimeType = Dayjs;\n\ntype FormaterOptions = {\n localeMatcher?: 'best fit' | 'lookup';\n weekday?: 'long' | 'short' | 'narrow';\n era?: 'long' | 'short' | 'narrow';\n year?: 'numeric' | '2-digit';\n month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';\n day?: 'numeric' | '2-digit';\n hour?: 'numeric' | '2-digit';\n minute?: 'numeric' | '2-digit';\n second?: 'numeric' | '2-digit';\n timeZoneName?: 'short' | 'long' | 'shortOffset' | 'longOffset' | 'shortGeneric' | 'longGeneric';\n formatMatcher?: 'best fit' | 'basic';\n hour12?: boolean;\n timeZone?: string;\n};\n\nconst dateOptions: FormaterOptions = {\n day: 'numeric',\n month: 'numeric',\n year: 'numeric',\n};\nconst timeOptions: FormaterOptions = {\n hour: 'numeric',\n minute: 'numeric',\n};\nconst dateTimeOptions: FormaterOptions = {\n ...dateOptions,\n ...timeOptions,\n};\n\nexport class DateHelper {\n private constructor(private instance: IDateTimeType | null) {}\n\n public static fromDateTimeString(source: string) {\n return new DateHelper(dayjsInstance(source));\n }\n\n public static from(source: Date | IDateTimeType | null | undefined) {\n if (!source) {\n return new DateHelper(null);\n }\n if (source instanceof Date) {\n return new DateHelper(dayjsInstance(source));\n }\n\n return new DateHelper(dayjsInstance(source));\n }\n\n public static now() {\n return new DateHelper(dayjsInstance());\n }\n\n public static addHours(source: IDateTimeType | Date | null, hours: number) {\n if (source === null || hours === 0) {\n return source;\n }\n\n return DateHelper.from(source).addHours(hours).getObj();\n }\n\n public addYears(years: number, update = true) {\n return this.add('years', years, update);\n }\n\n public addMonths(months: number, update = true) {\n return this.add('months', months, update);\n }\n\n public addWeeks(weeks: number, update = true) {\n return this.add('weeks', weeks, update);\n }\n\n public addDays(days: number, update = true) {\n return this.add('days', days, update);\n }\n\n public addHours(hours: number, update = true) {\n return this.add('hours', hours, update);\n }\n\n public addMinutes(minutes: number, update = true) {\n return this.add('minutes', minutes, update);\n }\n\n public addSeconds(seconds: number, update = true) {\n return this.add('seconds', seconds, update);\n }\n\n public addMilliseconds(milliseconds: number, update = true) {\n return this.add('milliseconds', milliseconds, update);\n }\n\n public getObj() {\n return this.instance;\n }\n\n public isAfter(\n before: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null || before === null) {\n return false;\n }\n return this.instance.isAfter(\n before instanceof DateHelper ? before.getObj() : DateHelper.from(before).getObj(),\n unit\n );\n }\n\n public isEqual(\n equal: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null && equal === null) {\n return true;\n } else if (this.instance === null || equal === null) {\n return false;\n }\n\n return this.instance.isSame(\n equal instanceof DateHelper ? equal.getObj() : DateHelper.from(equal).getObj(),\n unit\n );\n }\n\n public toDate() {\n return this.instance?.toDate() ?? null;\n }\n\n public getStartOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.startOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public getEndOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.endOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public diff(\n source: IDateTimeType | Date | null | DateHelper,\n unit: 'days' | 'hours' | 'minutes',\n float = true\n ) {\n if (this.instance === null || source === null) {\n return 0;\n }\n return this.instance.diff(\n source instanceof DateHelper ? source.getObj() : DateHelper.from(source).getObj(),\n unit,\n float\n );\n }\n\n private add(\n type: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds' | 'milliseconds',\n value: number,\n update = true\n ) {\n if (this.instance !== null) {\n const updated = this.instance.add(value, type);\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public format(format: string, defaultValue: string | null = null) {\n return this.instance?.format(format) ?? defaultValue;\n }\n\n public formatLocalDate(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...dateOptions, ...options }).format(date);\n }\n\n public formatLocalTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...timeOptions, ...options }).format(date);\n }\n\n public formatLocalDateTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...dateTimeOptions, ...options }).format(date);\n }\n\n public getLocale() {\n return this.instance?.locale() ?? null;\n }\n\n /**\n * Have you imported the locale file before setting the locale?\n * @param locale\n */\n public setLocale(locale: string) {\n if (this.instance !== null) {\n this.instance = this.instance.locale(locale);\n }\n\n return this;\n }\n}\n"],"mappings":"y0BAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAqC,oBACrCC,EAAsB,qCACtBC,EAAqB,oCACrBC,EAAgB,+BAEhB,EAAAC,QAAc,OAAO,EAAAC,OAAS,EAC9B,EAAAD,QAAc,OAAO,EAAAE,OA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W,KAAKE,CAAM,EAAE,OAAO,EAChFW,EACAK,CACF,CACF,CAEQ,IACNC,EACAC,EACAf,EAAS,GACT,CACA,GAAI,KAAK,WAAa,KAAM,CAC1B,IAAMY,EAAU,KAAK,SAAS,IAAIG,EAAOD,CAAI,EAE7C,OAAId,GACF,KAAK,SAAWY,EAET,MAEAjB,EAAW,KAAKiB,CAAO,CAElC,KACE,QAAO,IAEX,CAEO,OAAOI,EAAgBC,EAA8B,KAAM,CAjOpE,IAAAP,EAAAC,EAkOI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,OAAOM,KAAtB,KAAAL,EAAiCM,CAC1C,CAEO,gBAAgBC,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKH,GAAgB6B,EAAS,EAAE,OAAOC,CAAI,CACpF,CAEO,gBAAgBF,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKF,GAAgB4B,EAAS,EAAE,OAAOC,CAAI,CACpF,CAEO,oBAAoBF,EAAiBC,EAA2B,CACrE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKD,GAAoB2B,EAAS,EAAE,OAAOC,CAAI,CACxF,CAEO,WAAY,CA7PrB,IAAAV,EAAAC,EA8PI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,WAAf,KAAAC,EAA2B,IACpC,CAMO,UAAUO,EAAgB,CAC/B,OAAI,KAAK,WAAa,OACpB,KAAK,SAAW,KAAK,SAAS,OAAOA,CAAM,GAGtC,IACT,CACF","names":["index_exports","__export","DateHelper","__toCommonJS","import_dayjs","import_isBetween","import_timezone","import_utc","dayjsInstance","isBetween","utc","timezone","dateOptions","timeOptions","dateTimeOptions","__spreadValues","DateHelper","_DateHelper","instance","source","hours","years","update","months","weeks","days","minutes","seconds","milliseconds","before","unit","equal","_a","_b","updated","float","type","value","format","defaultValue","locale","options","date"]}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var u=Object.getOwnPropertySymbols;var f=Object.prototype.hasOwnProperty,h=Object.prototype.propertyIsEnumerable;var l=(n,t,e)=>t in n?d(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,r=(n,t)=>{for(var e in t||(t={}))f.call(t,e)&&l(n,e,t[e]);if(u)for(var e of u(t))h.call(t,e)&&l(n,e,t[e]);return n};import s from"dayjs";import p from"dayjs/plugin/isBetween";import b from"dayjs/plugin/timezone";import g from"dayjs/plugin/utc";s.extend(p);s.extend(g);s.extend(b);var c={day:"numeric",month:"numeric",year:"numeric"},m={hour:"numeric",minute:"numeric"},D=r(r({},c),m),o=class n{constructor(t){this.instance=t}static fromDateTimeString(t){return new n(s(t))}static from(t){return t?t instanceof Date?new n(s(t)):new n(s(t)):new n(null)}static now(){return new n(s())}static addHours(t,e){return t===null||e===0?t:n.from(t).addHours(e).getObj()}addDays(t,e=!0){return this.add("days",t,e)}addHours(t,e=!0){return this.add("hours",t,e)}getObj(){return this.instance}isAfter(t,e="milliseconds"){return this.instance===null||t===null?0:this.instance.isAfter(t instanceof n?t.getObj():n.from(t).getObj(),e)}isEqual(t,e="milliseconds"){return this.instance===null&&t===null?!0:this.instance===null||t===null?!1:this.instance.isSame(t instanceof n?t.getObj():n.from(t).getObj(),e)}toDate(){var t,e;return(e=(t=this.instance)==null?void 0:t.toDate())!=null?e:null}getStartOfDay(t=!0){if(this.instance!==null){let e=this.instance.startOf("day");return t?(this.instance=e,this):n.from(e)}else return this}getEndOfDay(t=!0){if(this.instance!==null){let e=this.instance.endOf("day");return t?(this.instance=e,this):n.from(e)}else return this}diff(t,e,i=!0){return this.instance===null||t===null?0:this.instance.diff(t instanceof n?t.getObj():n.from(t).getObj(),e,i)}add(t,e,i=!0){if(this.instance!==null){let a=this.instance.add(e,t);return i?(this.instance=a,this):n.from(a)}else return this}format(t,e=null){var i,a;return(a=(i=this.instance)==null?void 0:i.format(t))!=null?a:e}formatLocalDate(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},c),e)).format(i)}formatLocalTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},m),e)).format(i)}formatLocalDateTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},D),e)).format(i)}getLocale(){var t,e;return(e=(t=this.instance)==null?void 0:t.locale())!=null?e:null}setLocale(t){return this.instance!==null&&(this.instance=this.instance.locale(t)),this}};export{o as DateHelper};
1
+ var d=Object.defineProperty;var u=Object.getOwnPropertySymbols;var h=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable;var l=(n,t,e)=>t in n?d(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,r=(n,t)=>{for(var e in t||(t={}))h.call(t,e)&&l(n,e,t[e]);if(u)for(var e of u(t))f.call(t,e)&&l(n,e,t[e]);return n};import s from"dayjs";import p from"dayjs/plugin/isBetween";import b from"dayjs/plugin/timezone";import g from"dayjs/plugin/utc";s.extend(p);s.extend(g);s.extend(b);var c={day:"numeric",month:"numeric",year:"numeric"},m={hour:"numeric",minute:"numeric"},y=r(r({},c),m),o=class n{constructor(t){this.instance=t}static fromDateTimeString(t){return new n(s(t))}static from(t){return t?t instanceof Date?new n(s(t)):new n(s(t)):new n(null)}static now(){return new n(s())}static addHours(t,e){return t===null||e===0?t:n.from(t).addHours(e).getObj()}addYears(t,e=!0){return this.add("years",t,e)}addMonths(t,e=!0){return this.add("months",t,e)}addWeeks(t,e=!0){return this.add("weeks",t,e)}addDays(t,e=!0){return this.add("days",t,e)}addHours(t,e=!0){return this.add("hours",t,e)}addMinutes(t,e=!0){return this.add("minutes",t,e)}addSeconds(t,e=!0){return this.add("seconds",t,e)}addMilliseconds(t,e=!0){return this.add("milliseconds",t,e)}getObj(){return this.instance}isAfter(t,e="milliseconds"){return this.instance===null||t===null?!1:this.instance.isAfter(t instanceof n?t.getObj():n.from(t).getObj(),e)}isEqual(t,e="milliseconds"){return this.instance===null&&t===null?!0:this.instance===null||t===null?!1:this.instance.isSame(t instanceof n?t.getObj():n.from(t).getObj(),e)}toDate(){var t,e;return(e=(t=this.instance)==null?void 0:t.toDate())!=null?e:null}getStartOfDay(t=!0){if(this.instance!==null){let e=this.instance.startOf("day");return t?(this.instance=e,this):n.from(e)}else return this}getEndOfDay(t=!0){if(this.instance!==null){let e=this.instance.endOf("day");return t?(this.instance=e,this):n.from(e)}else return this}diff(t,e,i=!0){return this.instance===null||t===null?0:this.instance.diff(t instanceof n?t.getObj():n.from(t).getObj(),e,i)}add(t,e,i=!0){if(this.instance!==null){let a=this.instance.add(e,t);return i?(this.instance=a,this):n.from(a)}else return this}format(t,e=null){var i,a;return(a=(i=this.instance)==null?void 0:i.format(t))!=null?a:e}formatLocalDate(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},c),e)).format(i)}formatLocalTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},m),e)).format(i)}formatLocalDateTime(t,e){let i=this.toDate();return i===null?"":new Intl.DateTimeFormat(t,r(r({},y),e)).format(i)}getLocale(){var t,e;return(e=(t=this.instance)==null?void 0:t.locale())!=null?e:null}setLocale(t){return this.instance!==null&&(this.instance=this.instance.locale(t)),this}};export{o as DateHelper};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/lib/DateHelper.ts"],"sourcesContent":["import dayjsInstance, { Dayjs } from 'dayjs';\nimport isBetween from 'dayjs/plugin/isBetween'\nimport timezone from 'dayjs/plugin/timezone'\nimport utc from 'dayjs/plugin/utc'\n\ndayjsInstance.extend(isBetween);\ndayjsInstance.extend(utc);\ndayjsInstance.extend(timezone);\n\nexport type IDateTimeType = Dayjs;\n\ntype FormaterOptions = {\n localeMatcher?: \"best fit\" | \"lookup\";\n weekday?: \"long\" | \"short\" | \"narrow\";\n era?: \"long\" | \"short\" | \"narrow\";\n year?: \"numeric\" | \"2-digit\";\n month?: \"numeric\" | \"2-digit\" | \"long\" | \"short\" | \"narrow\";\n day?: \"numeric\" | \"2-digit\";\n hour?: \"numeric\" | \"2-digit\";\n minute?: \"numeric\" | \"2-digit\";\n second?: \"numeric\" | \"2-digit\";\n timeZoneName?: \"short\" | \"long\" | \"shortOffset\" | \"longOffset\" | \"shortGeneric\" | \"longGeneric\";\n formatMatcher?: \"best fit\" | \"basic\";\n hour12?: boolean;\n timeZone?: string;\n}\n\nconst dateOptions: FormaterOptions = {\n day: \"numeric\",month: \"numeric\", year: \"numeric\"\n}\nconst timeOptions: FormaterOptions = {\n hour: \"numeric\",minute: \"numeric\"\n}\nconst dateTimeOptions: FormaterOptions = {\n ...dateOptions,\n ...timeOptions\n}\n\nexport class DateHelper {\n private constructor(private instance: IDateTimeType | null) {}\n\n public static fromDateTimeString(source: string) {\n return new DateHelper(dayjsInstance(source));\n }\n\n public static from(source: Date | IDateTimeType | null | undefined) {\n if (!source) {\n return new DateHelper(null);\n }\n if (source instanceof Date) {\n return new DateHelper(dayjsInstance(source));\n }\n\n return new DateHelper(dayjsInstance(source));\n }\n\n public static now() {\n return new DateHelper(dayjsInstance());\n }\n\n public static addHours(source: IDateTimeType | Date | null, hours: number) {\n if (source === null || hours === 0) {\n return source;\n }\n\n return DateHelper.from(source).addHours(hours).getObj();\n }\n\n public addDays(days: number, update = true) {\n return this.add('days', days, update);\n }\n\n public addHours(hours: number, update = true) {\n return this.add('hours', hours, update);\n }\n\n public getObj() {\n return this.instance;\n }\n\n public isAfter(\n before: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null || before === null) {\n return 0;\n }\n return this.instance.isAfter(\n before instanceof DateHelper ? before.getObj() : DateHelper.from(before).getObj(),\n unit\n );\n }\n\n public isEqual(\n equal: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null && equal === null) {\n return true;\n } else if (this.instance === null || equal === null) {\n return false;\n }\n\n return this.instance.isSame(equal instanceof DateHelper ? equal.getObj() : DateHelper.from(equal).getObj(), unit);\n }\n\n public toDate() {\n return this.instance?.toDate() ?? null;\n }\n\n public getStartOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.startOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public getEndOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.endOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public diff(source: IDateTimeType | Date | null | DateHelper, unit: 'days' | 'hours' | 'minutes', float = true) {\n if (this.instance === null || source === null) {\n return 0;\n }\n return this.instance.diff(\n source instanceof DateHelper ? source.getObj() : DateHelper.from(source).getObj(),\n unit,\n float\n );\n }\n\n private add(type: 'days' | 'hours', value: number, update = true) {\n if (this.instance !== null) {\n const updated = this.instance.add(value, type);\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public format(format: string, defaultValue:string |null= null) {\n return this.instance?.format(format) ?? defaultValue\n }\n\n public formatLocalDate(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...dateOptions, ...options}).format(date)\n }\n\n public formatLocalTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...timeOptions, ...options}).format(date)\n }\n\n public formatLocalDateTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return \"\"\n }\n return new Intl.DateTimeFormat(locale, {...dateTimeOptions, ...options}).format(date)\n }\n\n public getLocale() {\n return this.instance?.locale() ?? null;\n }\n\n /**\n * Do you heave imported file with locale before setting of locale?\n * @param locale\n */\n public setLocale(locale: string) {\n if (this.instance !== null) {\n this.instance = this.instance.locale(locale);\n }\n\n return this;\n }\n}\n"],"mappings":"yVAAA,OAAOA,MAA8B,QACrC,OAAOC,MAAe,yBACtB,OAAOC,MAAc,wBACrB,OAAOC,MAAS,mBAEhBC,EAAc,OAAOC,CAAS,EAC9BD,EAAc,OAAOE,CAAG,EACxBF,EAAc,OAAOG,CAAQ,EAoB7B,IAAMC,EAA+B,CACnC,IAAK,UAAU,MAAO,UAAW,KAAM,SACzC,EACMC,EAA+B,CACnC,KAAM,UAAU,OAAQ,SAC1B,EACMC,EAAmCC,IAAA,GACpCH,GACAC,GAGQG,EAAN,MAAMC,CAAW,CACd,YAAoBC,EAAgC,CAAhC,cAAAA,CAAiC,CAE7D,OAAc,mBAAmBC,EAAgB,CAC/C,OAAO,IAAIF,EAAWT,EAAcW,CAAM,CAAC,CAC7C,CAEA,OAAc,KAAKA,EAAiD,CAClE,OAAKA,EAGDA,aAAkB,KACb,IAAIF,EAAWT,EAAcW,CAAM,CAAC,EAGtC,IAAIF,EAAWT,EAAcW,CAAM,CAAC,EANlC,IAAIF,EAAW,IAAI,CAO9B,CAEA,OAAc,KAAM,CAClB,OAAO,IAAIA,EAAWT,EAAc,CAAC,CACvC,CAEA,OAAc,SAASW,EAAqCC,EAAe,CACzE,OAAID,IAAW,MAAQC,IAAU,EACxBD,EAGFF,EAAW,KAAKE,CAAM,EAAE,SAASC,CAAK,EAAE,OAAO,CACxD,CAEO,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Options","__spreadValues","DateHelper","_DateHelper","instance","source","hours","days","update","before","unit","equal","_a","_b","updated","float","type","value","format","defaultValue","locale","options","date"]}
1
+ {"version":3,"sources":["../src/lib/DateHelper.ts"],"sourcesContent":["import dayjsInstance, { Dayjs } from 'dayjs';\nimport isBetween from 'dayjs/plugin/isBetween';\nimport timezone from 'dayjs/plugin/timezone';\nimport utc from 'dayjs/plugin/utc';\n\ndayjsInstance.extend(isBetween);\ndayjsInstance.extend(utc);\ndayjsInstance.extend(timezone);\n\nexport type IDateTimeType = Dayjs;\n\ntype FormaterOptions = {\n localeMatcher?: 'best fit' | 'lookup';\n weekday?: 'long' | 'short' | 'narrow';\n era?: 'long' | 'short' | 'narrow';\n year?: 'numeric' | '2-digit';\n month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow';\n day?: 'numeric' | '2-digit';\n hour?: 'numeric' | '2-digit';\n minute?: 'numeric' | '2-digit';\n second?: 'numeric' | '2-digit';\n timeZoneName?: 'short' | 'long' | 'shortOffset' | 'longOffset' | 'shortGeneric' | 'longGeneric';\n formatMatcher?: 'best fit' | 'basic';\n hour12?: boolean;\n timeZone?: string;\n};\n\nconst dateOptions: FormaterOptions = {\n day: 'numeric',\n month: 'numeric',\n year: 'numeric',\n};\nconst timeOptions: FormaterOptions = {\n hour: 'numeric',\n minute: 'numeric',\n};\nconst dateTimeOptions: FormaterOptions = {\n ...dateOptions,\n ...timeOptions,\n};\n\nexport class DateHelper {\n private constructor(private instance: IDateTimeType | null) {}\n\n public static fromDateTimeString(source: string) {\n return new DateHelper(dayjsInstance(source));\n }\n\n public static from(source: Date | IDateTimeType | null | undefined) {\n if (!source) {\n return new DateHelper(null);\n }\n if (source instanceof Date) {\n return new DateHelper(dayjsInstance(source));\n }\n\n return new DateHelper(dayjsInstance(source));\n }\n\n public static now() {\n return new DateHelper(dayjsInstance());\n }\n\n public static addHours(source: IDateTimeType | Date | null, hours: number) {\n if (source === null || hours === 0) {\n return source;\n }\n\n return DateHelper.from(source).addHours(hours).getObj();\n }\n\n public addYears(years: number, update = true) {\n return this.add('years', years, update);\n }\n\n public addMonths(months: number, update = true) {\n return this.add('months', months, update);\n }\n\n public addWeeks(weeks: number, update = true) {\n return this.add('weeks', weeks, update);\n }\n\n public addDays(days: number, update = true) {\n return this.add('days', days, update);\n }\n\n public addHours(hours: number, update = true) {\n return this.add('hours', hours, update);\n }\n\n public addMinutes(minutes: number, update = true) {\n return this.add('minutes', minutes, update);\n }\n\n public addSeconds(seconds: number, update = true) {\n return this.add('seconds', seconds, update);\n }\n\n public addMilliseconds(milliseconds: number, update = true) {\n return this.add('milliseconds', milliseconds, update);\n }\n\n public getObj() {\n return this.instance;\n }\n\n public isAfter(\n before: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null || before === null) {\n return false;\n }\n return this.instance.isAfter(\n before instanceof DateHelper ? before.getObj() : DateHelper.from(before).getObj(),\n unit\n );\n }\n\n public isEqual(\n equal: IDateTimeType | Date | null | DateHelper,\n unit:\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'months'\n | 'years'\n | 'dates'\n | 'weeks' = 'milliseconds'\n ) {\n if (this.instance === null && equal === null) {\n return true;\n } else if (this.instance === null || equal === null) {\n return false;\n }\n\n return this.instance.isSame(\n equal instanceof DateHelper ? equal.getObj() : DateHelper.from(equal).getObj(),\n unit\n );\n }\n\n public toDate() {\n return this.instance?.toDate() ?? null;\n }\n\n public getStartOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.startOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public getEndOfDay(update = true) {\n if (this.instance !== null) {\n const updated = this.instance.endOf('day');\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public diff(\n source: IDateTimeType | Date | null | DateHelper,\n unit: 'days' | 'hours' | 'minutes',\n float = true\n ) {\n if (this.instance === null || source === null) {\n return 0;\n }\n return this.instance.diff(\n source instanceof DateHelper ? source.getObj() : DateHelper.from(source).getObj(),\n unit,\n float\n );\n }\n\n private add(\n type: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds' | 'milliseconds',\n value: number,\n update = true\n ) {\n if (this.instance !== null) {\n const updated = this.instance.add(value, type);\n\n if (update) {\n this.instance = updated;\n\n return this;\n } else {\n return DateHelper.from(updated);\n }\n } else {\n return this;\n }\n }\n\n public format(format: string, defaultValue: string | null = null) {\n return this.instance?.format(format) ?? defaultValue;\n }\n\n public formatLocalDate(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...dateOptions, ...options }).format(date);\n }\n\n public formatLocalTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...timeOptions, ...options }).format(date);\n }\n\n public formatLocalDateTime(locale?: string, options?: FormaterOptions) {\n const date = this.toDate();\n if (date === null) {\n return '';\n }\n return new Intl.DateTimeFormat(locale, { ...dateTimeOptions, ...options }).format(date);\n }\n\n public getLocale() {\n return this.instance?.locale() ?? null;\n }\n\n /**\n * Have you imported the locale file before setting the locale?\n * @param locale\n */\n public setLocale(locale: string) {\n if (this.instance !== null) {\n this.instance = this.instance.locale(locale);\n }\n\n return this;\n }\n}\n"],"mappings":"yVAAA,OAAOA,MAA8B,QACrC,OAAOC,MAAe,yBACtB,OAAOC,MAAc,wBACrB,OAAOC,MAAS,mBAEhBC,EAAc,OAAOC,CAAS,EAC9BD,EAAc,OAAOE,CAAG,EACxBF,EAAc,OAAOG,CAAQ,EAoB7B,IAAMC,EAA+B,CACnC,IAAK,UACL,MAAO,UAC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CAC1B,IAAMY,EAAU,KAAK,SAAS,IAAIG,EAAOD,CAAI,EAE7C,OAAId,GACF,KAAK,SAAWY,EAET,MAEAjB,EAAW,KAAKiB,CAAO,CAElC,KACE,QAAO,IAEX,CAEO,OAAOI,EAAgBC,EAA8B,KAAM,CAjOpE,IAAAP,EAAAC,EAkOI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,OAAOM,KAAtB,KAAAL,EAAiCM,CAC1C,CAEO,gBAAgBC,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKH,GAAgB6B,EAAS,EAAE,OAAOC,CAAI,CACpF,CAEO,gBAAgBF,EAAiBC,EAA2B,CACjE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKF,GAAgB4B,EAAS,EAAE,OAAOC,CAAI,CACpF,CAEO,oBAAoBF,EAAiBC,EAA2B,CACrE,IAAMC,EAAO,KAAK,OAAO,EACzB,OAAIA,IAAS,KACJ,GAEF,IAAI,KAAK,eAAeF,EAAQzB,IAAA,GAAKD,GAAoB2B,EAAS,EAAE,OAAOC,CAAI,CACxF,CAEO,WAAY,CA7PrB,IAAAV,EAAAC,EA8PI,OAAOA,GAAAD,EAAA,KAAK,WAAL,YAAAA,EAAe,WAAf,KAAAC,EAA2B,IACpC,CAMO,UAAUO,EAAgB,CAC/B,OAAI,KAAK,WAAa,OACpB,KAAK,SAAW,KAAK,SAAS,OAAOA,CAAM,GAGtC,IACT,CACF","names":["dayjsInstance","isBetween","timezone","utc","dayjsInstance","isBetween","utc","timezone","dateOptions","timeOptions","dateTimeOptions","__spreadValues","DateHelper","_DateHelper","instance","source","hours","years","update","months","weeks","days","minutes","seconds","milliseconds","before","unit","equal","_a","_b","updated","float","type","value","format","defaultValue","locale","options","date"]}
package/package.json CHANGED
@@ -1,12 +1,33 @@
1
1
  {
2
2
  "name": "@connect-soft/date-helper",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
+ "description": "A TypeScript date manipulation library built on top of Day.js, providing a fluent API for working with dates, times, and locales",
5
+ "keywords": [
6
+ "date",
7
+ "datetime",
8
+ "dayjs",
9
+ "date-helper",
10
+ "time",
11
+ "locale",
12
+ "timezone",
13
+ "format",
14
+ "typescript"
15
+ ],
16
+ "author": "Connect Soft",
4
17
  "license": "MIT",
18
+ "homepage": "https://gitlab.com/connect-soft/date-helper#readme",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://gitlab.com/connect-soft/date-helper.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://gitlab.com/connect-soft/date-helper/issues"
25
+ },
5
26
  "exports": {
6
27
  ".": {
28
+ "types": "./dist/index.d.ts",
7
29
  "import": "./dist/index.mjs",
8
- "require": "./dist/index.cjs",
9
- "types": "./dist/index.d.ts"
30
+ "require": "./dist/index.cjs"
10
31
  }
11
32
  },
12
33
  "main": "./dist/index.cjs",
@@ -21,10 +42,14 @@
21
42
  "@jest/globals": "30.2.0",
22
43
  "@swc/core": "^1.11.18",
23
44
  "@types/jest": "30.0.0",
45
+ "@typescript-eslint/eslint-plugin": "^8.19.1",
46
+ "@typescript-eslint/parser": "^8.19.1",
24
47
  "dayjs": "^1.11.13",
48
+ "eslint": "^9.18.0",
25
49
  "husky": "^9.1.7",
26
50
  "jest": "30.2.0",
27
51
  "json-sort-cli": "^4.0.9",
52
+ "prettier": "^3.4.2",
28
53
  "ts-jest": "29.4.6",
29
54
  "ts-node": "10.9.2",
30
55
  "tsup": "^8.4.0",
@@ -34,9 +59,15 @@
34
59
  "dayjs": "^1.11.13"
35
60
  },
36
61
  "scripts": {
37
- "build": "pnpm types && tsup src/index.ts --format esm,cjs --dts --minify --outDir dist --sourcemap",
62
+ "build": "tsup src/index.ts --format esm,cjs --dts --minify --outDir dist --sourcemap",
38
63
  "clean": "rm -fr dist",
64
+ "lint": "eslint 'src/**/*.{ts,tsx}'",
65
+ "lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix",
66
+ "format": "prettier --write 'src/**/*.{ts,tsx,json,md}'",
67
+ "format:check": "prettier --check 'src/**/*.{ts,tsx,json,md}'",
39
68
  "sort": "sortjson",
40
- "types": "tsc --outDir dist"
69
+ "test": "jest",
70
+ "test:coverage": "jest --coverage",
71
+ "typecheck": "tsc --noEmit"
41
72
  }
42
73
  }
@@ -1,46 +0,0 @@
1
- import dayjsInstance, { Dayjs } from 'dayjs';
2
- export type IDateTimeType = Dayjs;
3
- type FormaterOptions = {
4
- localeMatcher?: "best fit" | "lookup";
5
- weekday?: "long" | "short" | "narrow";
6
- era?: "long" | "short" | "narrow";
7
- year?: "numeric" | "2-digit";
8
- month?: "numeric" | "2-digit" | "long" | "short" | "narrow";
9
- day?: "numeric" | "2-digit";
10
- hour?: "numeric" | "2-digit";
11
- minute?: "numeric" | "2-digit";
12
- second?: "numeric" | "2-digit";
13
- timeZoneName?: "short" | "long" | "shortOffset" | "longOffset" | "shortGeneric" | "longGeneric";
14
- formatMatcher?: "best fit" | "basic";
15
- hour12?: boolean;
16
- timeZone?: string;
17
- };
18
- export declare class DateHelper {
19
- private instance;
20
- private constructor();
21
- static fromDateTimeString(source: string): DateHelper;
22
- static from(source: Date | IDateTimeType | null | undefined): DateHelper;
23
- static now(): DateHelper;
24
- static addHours(source: IDateTimeType | Date | null, hours: number): dayjsInstance.Dayjs | Date | null;
25
- addDays(days: number, update?: boolean): DateHelper;
26
- addHours(hours: number, update?: boolean): DateHelper;
27
- getObj(): dayjsInstance.Dayjs | null;
28
- isAfter(before: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean | 0;
29
- isEqual(equal: IDateTimeType | Date | null | DateHelper, unit?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates' | 'weeks'): boolean;
30
- toDate(): Date | null;
31
- getStartOfDay(update?: boolean): DateHelper;
32
- getEndOfDay(update?: boolean): DateHelper;
33
- diff(source: IDateTimeType | Date | null | DateHelper, unit: 'days' | 'hours' | 'minutes', float?: boolean): number;
34
- private add;
35
- format(format: string, defaultValue?: string | null): string | null;
36
- formatLocalDate(locale?: string, options?: FormaterOptions): string;
37
- formatLocalTime(locale?: string, options?: FormaterOptions): string;
38
- formatLocalDateTime(locale?: string, options?: FormaterOptions): string;
39
- getLocale(): string | null;
40
- /**
41
- * Do you heave imported file with locale before setting of locale?
42
- * @param locale
43
- */
44
- setLocale(locale: string): this;
45
- }
46
- export {};