@libs-ui/services-format-date 0.2.355-8 → 0.2.356-0

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.
Files changed (2) hide show
  1. package/README.md +100 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,3 +1,101 @@
1
- # services-format-date
1
+ # Format Date Service
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ Service định dạng ngày giờ dựa trên dayjs. Thin wrapper của `formatDate()` từ `@libs-ui/utils`. Hỗ trợ nhiều kiểu input, định dạng tùy chỉnh, đa ngôn ngữ (vi/en) và format aliases locale-aware.
4
+
5
+ ## Tính năng
6
+
7
+ - ✅ Input types đa dạng: Unix timestamp (giây), ISO string, dayjs object, Date
8
+ - ✅ Format aliases locale-aware: `dmy`, `dmy hm`, `dmy hms`, `my`, `dm`... tự chọn format theo ngôn ngữ
9
+ - ✅ Đa ngôn ngữ: vi/en — tiếng Việt ngày trước tháng, tiếng Anh tháng trước ngày
10
+ - ✅ Tự lấy ngôn ngữ từ `UtilsCache.getLang()` nếu không truyền
11
+ - ✅ null/undefined → trả về `''` (không throw error)
12
+ - ✅ Custom format override hook toàn cục (`updateFunctionFormatDate`)
13
+
14
+ ## Cài đặt
15
+
16
+ ```bash
17
+ npm install @libs-ui/services-format-date
18
+ ```
19
+
20
+ ## Import
21
+
22
+ ```typescript
23
+ import { FormatDateService } from '@libs-ui/services-format-date';
24
+ ```
25
+
26
+ ## Cách sử dụng
27
+
28
+ ### Trong Component
29
+
30
+ ```typescript
31
+ import { Component, inject } from '@angular/core';
32
+ import { FormatDateService } from '@libs-ui/services-format-date';
33
+
34
+ @Component({
35
+ standalone: true,
36
+ template: `
37
+ <p>{{ formatDateService.transform(timestamp) }}</p>
38
+ <p>{{ formatDateService.transform(date, 'dmy hm', 'vi') }}</p>
39
+ `,
40
+ })
41
+ export class ExampleComponent {
42
+ readonly formatDateService = inject(FormatDateService);
43
+
44
+ timestamp = 1716222600; // Unix timestamp (giây)
45
+ date = '2024-05-20T10:30:00'; // ISO string
46
+ }
47
+ ```
48
+
49
+ ### Dùng trong TypeScript (khuyến nghị)
50
+
51
+ Service là thin wrapper của `formatDate` trong `@libs-ui/utils`. Khi cần dùng trong TypeScript, import thẳng hàm đó — **không cần inject service**:
52
+
53
+ ```typescript
54
+ import { formatDate } from '@libs-ui/utils';
55
+
56
+ // Không cần inject service
57
+ const result = formatDate(1716222600, 'YYYY/MM/DD HH:mm');
58
+ // Output: '2024/05/20 17:30'
59
+
60
+ const viDate = formatDate('2024-05-20', 'dmy hm', 'vi');
61
+ // Output: '20 Th5, 2024 00:00'
62
+
63
+ // Trong component class:
64
+ class ExampleComponent {
65
+ formattedDate = formatDate(this.rawDate, 'DD/MM/YYYY');
66
+ }
67
+ ```
68
+
69
+ ## API
70
+
71
+ ### `transform(time, format?, lang?)`
72
+
73
+ | Parameter | Type | Default | Mô tả |
74
+ | --------- | ------------------------------------------------------ | ---------------------- | ---------------------------------------- |
75
+ | `time` | `string \| number \| dayjs.Dayjs \| undefined \| null` | - | Giá trị thời gian. null/undefined → `''` |
76
+ | `format` | `string` | `'YYYY/MM/DD HH:mm'` | Format dayjs hoặc alias ngắn gọn |
77
+ | `lang` | `string \| undefined` | `UtilsCache.getLang()` | Ngôn ngữ: `'vi'` hoặc `'en'` |
78
+
79
+ **Returns:** `string` — chuỗi đã format, hoặc `''` nếu input là null/undefined.
80
+
81
+ ## Format Aliases (locale-aware)
82
+
83
+ | Alias | Format vi | Format en | Ghi chú |
84
+ | ---------- | -------------------------- | -------------------------- | ----------------------- |
85
+ | `dmy hm` | `D MMM, YYYY HH:mm` | `MMM D, YYYY HH:mm` | Ngày tháng năm giờ phút |
86
+ | `dmy` | `D MMM, YYYY` | `MMM D, YYYY` | Ngày tháng năm |
87
+ | `dm` | `D MMM` | `MMM D` | Ngày tháng |
88
+ | `my` | `MMM, YYYY` | `MMM YYYY` | Tháng năm |
89
+ | `dmy hms` | `D MMM, YYYY HH:mm:ss` | `MMM D, YYYY HH:mm:ss` | Có giây |
90
+ | `dmy hmsS` | `D MMM, YYYY HH:mm:ss:SSS` | `MMM D, YYYY HH:mm:ss:SSS` | Có millisecond |
91
+ | `HH:mm` | `HH:mm` | `HH:mm` | 24h format |
92
+ | `HH:mm A` | `HH:mm A` | `HH:mm A` | 12h AM/PM |
93
+
94
+ Format string trực tiếp (như `DD/MM/YYYY`, `YYYY-MM-DD`...) cũng được hỗ trợ — không locale-aware.
95
+
96
+ ## Lưu ý quan trọng
97
+
98
+ - **Inject service hay dùng thẳng utils?** Nếu chỉ cần trong TypeScript class, dùng thẳng `formatDate()` từ `@libs-ui/utils` — không inject service. Inject service khi cần DI (mock test) hoặc dùng trong template qua service reference.
99
+ - **lang** không truyền → tự lấy từ `UtilsCache.getLang()`. Đảm bảo set ngôn ngữ global trước khi dùng.
100
+ - **Unix timestamp** là số nguyên **giây** (không phải milliseconds). `Date.now()` trả về ms → chia 1000.
101
+ - **Format aliases** chỉ có tác dụng locale-aware khi dùng với `lang='vi'` hoặc `lang='en'`. Format string trực tiếp không phụ thuộc ngôn ngữ.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@libs-ui/services-format-date",
3
- "version": "0.2.355-8",
3
+ "version": "0.2.356-0",
4
4
  "peerDependencies": {
5
5
  "@angular/core": ">=18.0.0",
6
6
  "dayjs": "1.11.5",
7
- "@libs-ui/utils": "0.2.355-8"
7
+ "@libs-ui/utils": "0.2.356-0"
8
8
  },
9
9
  "sideEffects": false,
10
10
  "module": "fesm2022/libs-ui-services-format-date.mjs",