ts-date-lite 0.0.1-beta.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/dist/date.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ export declare class DateUtils {
2
+ format(date: Date, formatStr: string): string;
3
+ relative(date: Date): string;
4
+ isToday(date: Date): boolean;
5
+ add(date: Date, amount: number, unit: 'day' | 'month' | 'year'): Date;
6
+ diff(d1: Date, d2: Date): number;
7
+ startOf(date: Date, unit: 'day' | 'month' | 'year'): Date;
8
+ endOf(date: Date, unit: 'day' | 'month' | 'year'): Date;
9
+ isBefore(d1: Date, d2: Date): boolean;
10
+ isAfter(d1: Date, d2: Date): boolean;
11
+ isSame(d1: Date, d2: Date): boolean;
12
+ parse(dateStr: string): Date | null;
13
+ now(): number;
14
+ timestamp(): number;
15
+ }
package/dist/date.js ADDED
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DateUtils = void 0;
4
+ class DateUtils {
5
+ format(date, formatStr) {
6
+ const d = new Date(date);
7
+ const pad = (n) => n.toString().padStart(2, '0');
8
+ const year = d.getFullYear();
9
+ const month = pad(d.getMonth() + 1);
10
+ const day = pad(d.getDate());
11
+ const hours = pad(d.getHours());
12
+ const minutes = pad(d.getMinutes());
13
+ const seconds = pad(d.getSeconds());
14
+ return formatStr
15
+ .replace('YYYY', String(year))
16
+ .replace('MM', month)
17
+ .replace('DD', day)
18
+ .replace('HH', hours)
19
+ .replace('mm', minutes)
20
+ .replace('ss', seconds);
21
+ }
22
+ relative(date) {
23
+ const now = Date.now();
24
+ const diff = now - new Date(date).getTime();
25
+ const seconds = Math.floor(diff / 1000);
26
+ if (seconds < 60)
27
+ return `${seconds} seconds ago`;
28
+ const minutes = Math.floor(seconds / 60);
29
+ if (minutes < 60)
30
+ return `${minutes} minutes ago`;
31
+ const hours = Math.floor(minutes / 60);
32
+ if (hours < 24)
33
+ return `${hours} hours ago`;
34
+ const days = Math.floor(hours / 24);
35
+ if (days < 30)
36
+ return `${days} days ago`;
37
+ const months = Math.floor(days / 30);
38
+ if (months < 12)
39
+ return `${months} months ago`;
40
+ return `${Math.floor(days / 365)} years ago`;
41
+ }
42
+ isToday(date) {
43
+ const d = new Date(date);
44
+ const today = new Date();
45
+ return d.getDate() === today.getDate() && d.getMonth() === today.getMonth() && d.getFullYear() === today.getFullYear();
46
+ }
47
+ add(date, amount, unit) {
48
+ const d = new Date(date);
49
+ if (unit === 'day')
50
+ d.setDate(d.getDate() + amount);
51
+ else if (unit === 'month')
52
+ d.setMonth(d.getMonth() + amount);
53
+ else
54
+ d.setFullYear(d.getFullYear() + amount);
55
+ return d;
56
+ }
57
+ diff(d1, d2) {
58
+ return Math.floor((new Date(d1).getTime() - new Date(d2).getTime()) / (1000 * 60 * 60 * 24));
59
+ }
60
+ startOf(date, unit) {
61
+ const d = new Date(date);
62
+ if (unit === 'day')
63
+ d.setHours(0, 0, 0, 0);
64
+ else if (unit === 'month')
65
+ d.setDate(1);
66
+ else
67
+ d.setMonth(0, 1);
68
+ return d;
69
+ }
70
+ endOf(date, unit) {
71
+ const d = this.startOf(date, unit);
72
+ if (unit === 'day')
73
+ d.setHours(23, 59, 59, 999);
74
+ else if (unit === 'month')
75
+ d.setMonth(d.getMonth() + 1, 0);
76
+ else
77
+ d.setFullYear(d.getFullYear() + 1, 0, 0);
78
+ return d;
79
+ }
80
+ isBefore(d1, d2) { return new Date(d1).getTime() < new Date(d2).getTime(); }
81
+ isAfter(d1, d2) { return new Date(d1).getTime() > new Date(d2).getTime(); }
82
+ isSame(d1, d2) { return this.isBefore(d1, d2) === false && this.isAfter(d1, d2) === false; }
83
+ parse(dateStr) {
84
+ const d = new Date(dateStr);
85
+ return isNaN(d.getTime()) ? null : d;
86
+ }
87
+ now() { return Date.now(); }
88
+ timestamp() { return Date.now(); }
89
+ }
90
+ exports.DateUtils = DateUtils;
@@ -0,0 +1 @@
1
+ export * from './date';
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./date"), exports);
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "ts-date-lite",
3
+ "version": "0.0.1-beta.1",
4
+ "description": "Lightweight, type-safe date utility library for web developers",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "keywords": [
19
+ "utils",
20
+ "typescript",
21
+ "date",
22
+ "helpers",
23
+ "lite"
24
+ ],
25
+ "author": "Gaurang Mody - G8X",
26
+ "license": "MIT",
27
+ "devDependencies": {
28
+ "typescript": "^6.0.2"
29
+ }
30
+ }