vue-use-date-format 1.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.github.md +125 -0
- package/README.md +125 -0
- package/README.npm.md +125 -0
- package/dist/index.cjs +95 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.mts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.mjs +91 -0
- package/package.json +60 -0
package/README.github.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="banner.svg" alt="vue-use-date-format" width="100%" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">vue-use-date-format</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">A Vue 3 composition API utility for formatting dates reactively using format tokens (YYYY, MM, DD, HH, mm, ss, etc.). Supports localization and custom meridiem.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/v/vue-use-date-format.svg" alt="npm version" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/dm/vue-use-date-format.svg" alt="npm downloads" /></a>
|
|
12
|
+
<a href="https://github.com/vuefrag/vue-use-date-format/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/vue-use-date-format.svg" alt="license" /></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install vue-use-date-format
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { useDateFormat } from 'vue-use-date-format';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Get the formatted date according to the string of tokens passed in, inspired
|
|
29
|
+
by [dayjs](https://github.com/iamkun/dayjs).
|
|
30
|
+
|
|
31
|
+
**List of all available formats (HH:mm:ss by default):**
|
|
32
|
+
|
|
33
|
+
| Format | Output | Description |
|
|
34
|
+
| ------ | ------------------------ | --------------------------------------- |
|
|
35
|
+
| `Yo` | 2018th | Ordinal formatted year |
|
|
36
|
+
| `YY` | 18 | Two-digit year |
|
|
37
|
+
| `YYYY` | 2018 | Four-digit year |
|
|
38
|
+
| `M` | 1-12 | The month, beginning at 1 |
|
|
39
|
+
| `Mo` | 1st, 2nd, ..., 12th | The month, ordinal formatted |
|
|
40
|
+
| `MM` | 01-12 | The month, 2-digits |
|
|
41
|
+
| `MMM` | Jan-Dec | The abbreviated month name |
|
|
42
|
+
| `MMMM` | January-December | The full month name |
|
|
43
|
+
| `D` | 1-31 | The day of the month |
|
|
44
|
+
| `Do` | 1st, 2nd, ..., 31st | The day of the month, ordinal formatted |
|
|
45
|
+
| `DD` | 01-31 | The day of the month, 2-digits |
|
|
46
|
+
| `H` | 0-23 | The hour |
|
|
47
|
+
| `Ho` | 0th, 1st, 2nd, ..., 23rd | The hour, ordinal formatted |
|
|
48
|
+
| `HH` | 00-23 | The hour, 2-digits |
|
|
49
|
+
| `h` | 1-12 | The hour, 12-hour clock |
|
|
50
|
+
| `ho` | 1st, 2nd, ..., 12th | The hour, 12-hour clock, sorted |
|
|
51
|
+
| `hh` | 01-12 | The hour, 12-hour clock, 2-digits |
|
|
52
|
+
| `m` | 0-59 | The minute |
|
|
53
|
+
| `mo` | 0th, 1st, ..., 59th | The minute, ordinal formatted |
|
|
54
|
+
| `mm` | 00-59 | The minute, 2-digits |
|
|
55
|
+
| `s` | 0-59 | The second |
|
|
56
|
+
| `so` | 0th, 1st, ..., 59th | The second, ordinal formatted |
|
|
57
|
+
| `ss` | 00-59 | The second, 2-digits |
|
|
58
|
+
| `SSS` | 000-999 | The millisecond, 3-digits |
|
|
59
|
+
| `A` | AM PM | The meridiem |
|
|
60
|
+
| `AA` | A.M. P.M. | The meridiem, periods |
|
|
61
|
+
| `a` | am pm | The meridiem, lowercase |
|
|
62
|
+
| `aa` | a.m. p.m. | The meridiem, lowercase and periods |
|
|
63
|
+
| `d` | 0-6 | The day of the week, with Sunday as 0 |
|
|
64
|
+
| `dd` | S-S | The min name of the day of the week |
|
|
65
|
+
| `ddd` | Sun-Sat | The short name of the day of the week |
|
|
66
|
+
| `dddd` | Sunday-Saturday | The name of the day of the week |
|
|
67
|
+
| `z` | GMT, GMT+1 | The timezone with offset |
|
|
68
|
+
| `zz` | GMT, GMT+1 | The timezone with offset |
|
|
69
|
+
| `zzz` | GMT, GMT+1 | The timezone with offset |
|
|
70
|
+
| `zzzz` | GMT, GMT+01:00 | The long timezone with offset |
|
|
71
|
+
|
|
72
|
+
- Meridiem is customizable by defining `customMeridiem` in `options`.
|
|
73
|
+
|
|
74
|
+
### Basic
|
|
75
|
+
|
|
76
|
+
```vue
|
|
77
|
+
<script setup lang="ts">
|
|
78
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
79
|
+
|
|
80
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')
|
|
81
|
+
</script>
|
|
82
|
+
|
|
83
|
+
<template>
|
|
84
|
+
<div>{{ formatted }}</div>
|
|
85
|
+
</template>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Use with locales
|
|
89
|
+
|
|
90
|
+
```vue
|
|
91
|
+
<script setup lang="ts">
|
|
92
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
93
|
+
|
|
94
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD (ddd)', { locales: 'en-US' })
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<template>
|
|
98
|
+
<div>{{ formatted }}</div>
|
|
99
|
+
</template>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Use with custom meridiem
|
|
103
|
+
|
|
104
|
+
```vue
|
|
105
|
+
<script setup lang="ts">
|
|
106
|
+
import { useDateFormat } from 'vue-use-date-format'
|
|
107
|
+
|
|
108
|
+
function customMeridiem(hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) {
|
|
109
|
+
const m = hours > 11 ? (isLowercase ? 'μμ' : 'ΜΜ') : (isLowercase ? 'πμ' : 'ΠΜ')
|
|
110
|
+
return hasPeriod ? m.split('').reduce((acc, current) => acc += `${current}.`, '') : m
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const am = useDateFormat('2022-01-01 05:05:05', 'hh:mm:ss A', { customMeridiem })
|
|
114
|
+
// am.value = '05:05:05 ΠΜ'
|
|
115
|
+
const pm = useDateFormat('2022-01-01 17:05:05', 'hh:mm:ss AA', { customMeridiem })
|
|
116
|
+
// pm.value = '05:05:05 Μ.Μ.'
|
|
117
|
+
</script>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> Extracted from [VueUse](https://vueuse.org/) for standalone use.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo.svg" alt="vue-use-date-format" width="180" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">vue-use-date-format</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">A Vue 3 composition API utility for formatting dates reactively using format tokens (YYYY, MM, DD, HH, mm, ss, etc.). Supports localization and custom meridiem.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/v/vue-use-date-format.svg" alt="npm version" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/dm/vue-use-date-format.svg" alt="npm downloads" /></a>
|
|
12
|
+
<a href="https://github.com/vuefrag/vue-use-date-format/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/vue-use-date-format.svg" alt="license" /></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install vue-use-date-format
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { useDateFormat } from 'vue-use-date-format';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Get the formatted date according to the string of tokens passed in, inspired
|
|
29
|
+
by [dayjs](https://github.com/iamkun/dayjs).
|
|
30
|
+
|
|
31
|
+
**List of all available formats (HH:mm:ss by default):**
|
|
32
|
+
|
|
33
|
+
| Format | Output | Description |
|
|
34
|
+
| ------ | ------------------------ | --------------------------------------- |
|
|
35
|
+
| `Yo` | 2018th | Ordinal formatted year |
|
|
36
|
+
| `YY` | 18 | Two-digit year |
|
|
37
|
+
| `YYYY` | 2018 | Four-digit year |
|
|
38
|
+
| `M` | 1-12 | The month, beginning at 1 |
|
|
39
|
+
| `Mo` | 1st, 2nd, ..., 12th | The month, ordinal formatted |
|
|
40
|
+
| `MM` | 01-12 | The month, 2-digits |
|
|
41
|
+
| `MMM` | Jan-Dec | The abbreviated month name |
|
|
42
|
+
| `MMMM` | January-December | The full month name |
|
|
43
|
+
| `D` | 1-31 | The day of the month |
|
|
44
|
+
| `Do` | 1st, 2nd, ..., 31st | The day of the month, ordinal formatted |
|
|
45
|
+
| `DD` | 01-31 | The day of the month, 2-digits |
|
|
46
|
+
| `H` | 0-23 | The hour |
|
|
47
|
+
| `Ho` | 0th, 1st, 2nd, ..., 23rd | The hour, ordinal formatted |
|
|
48
|
+
| `HH` | 00-23 | The hour, 2-digits |
|
|
49
|
+
| `h` | 1-12 | The hour, 12-hour clock |
|
|
50
|
+
| `ho` | 1st, 2nd, ..., 12th | The hour, 12-hour clock, sorted |
|
|
51
|
+
| `hh` | 01-12 | The hour, 12-hour clock, 2-digits |
|
|
52
|
+
| `m` | 0-59 | The minute |
|
|
53
|
+
| `mo` | 0th, 1st, ..., 59th | The minute, ordinal formatted |
|
|
54
|
+
| `mm` | 00-59 | The minute, 2-digits |
|
|
55
|
+
| `s` | 0-59 | The second |
|
|
56
|
+
| `so` | 0th, 1st, ..., 59th | The second, ordinal formatted |
|
|
57
|
+
| `ss` | 00-59 | The second, 2-digits |
|
|
58
|
+
| `SSS` | 000-999 | The millisecond, 3-digits |
|
|
59
|
+
| `A` | AM PM | The meridiem |
|
|
60
|
+
| `AA` | A.M. P.M. | The meridiem, periods |
|
|
61
|
+
| `a` | am pm | The meridiem, lowercase |
|
|
62
|
+
| `aa` | a.m. p.m. | The meridiem, lowercase and periods |
|
|
63
|
+
| `d` | 0-6 | The day of the week, with Sunday as 0 |
|
|
64
|
+
| `dd` | S-S | The min name of the day of the week |
|
|
65
|
+
| `ddd` | Sun-Sat | The short name of the day of the week |
|
|
66
|
+
| `dddd` | Sunday-Saturday | The name of the day of the week |
|
|
67
|
+
| `z` | GMT, GMT+1 | The timezone with offset |
|
|
68
|
+
| `zz` | GMT, GMT+1 | The timezone with offset |
|
|
69
|
+
| `zzz` | GMT, GMT+1 | The timezone with offset |
|
|
70
|
+
| `zzzz` | GMT, GMT+01:00 | The long timezone with offset |
|
|
71
|
+
|
|
72
|
+
- Meridiem is customizable by defining `customMeridiem` in `options`.
|
|
73
|
+
|
|
74
|
+
### Basic
|
|
75
|
+
|
|
76
|
+
```vue
|
|
77
|
+
<script setup lang="ts">
|
|
78
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
79
|
+
|
|
80
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')
|
|
81
|
+
</script>
|
|
82
|
+
|
|
83
|
+
<template>
|
|
84
|
+
<div>{{ formatted }}</div>
|
|
85
|
+
</template>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Use with locales
|
|
89
|
+
|
|
90
|
+
```vue
|
|
91
|
+
<script setup lang="ts">
|
|
92
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
93
|
+
|
|
94
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD (ddd)', { locales: 'en-US' })
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<template>
|
|
98
|
+
<div>{{ formatted }}</div>
|
|
99
|
+
</template>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Use with custom meridiem
|
|
103
|
+
|
|
104
|
+
```vue
|
|
105
|
+
<script setup lang="ts">
|
|
106
|
+
import { useDateFormat } from 'vue-use-date-format'
|
|
107
|
+
|
|
108
|
+
function customMeridiem(hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) {
|
|
109
|
+
const m = hours > 11 ? (isLowercase ? 'μμ' : 'ΜΜ') : (isLowercase ? 'πμ' : 'ΠΜ')
|
|
110
|
+
return hasPeriod ? m.split('').reduce((acc, current) => acc += `${current}.`, '') : m
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const am = useDateFormat('2022-01-01 05:05:05', 'hh:mm:ss A', { customMeridiem })
|
|
114
|
+
// am.value = '05:05:05 ΠΜ'
|
|
115
|
+
const pm = useDateFormat('2022-01-01 17:05:05', 'hh:mm:ss AA', { customMeridiem })
|
|
116
|
+
// pm.value = '05:05:05 Μ.Μ.'
|
|
117
|
+
</script>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> Extracted from [VueUse](https://vueuse.org/) for standalone use.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
package/README.npm.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo.svg" alt="vue-use-date-format" width="180" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">vue-use-date-format</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">A Vue 3 composition API utility for formatting dates reactively using format tokens (YYYY, MM, DD, HH, mm, ss, etc.). Supports localization and custom meridiem.</p>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/v/vue-use-date-format.svg" alt="npm version" /></a>
|
|
11
|
+
<a href="https://www.npmjs.com/package/vue-use-date-format"><img src="https://img.shields.io/npm/dm/vue-use-date-format.svg" alt="npm downloads" /></a>
|
|
12
|
+
<a href="https://github.com/vuefrag/vue-use-date-format/blob/main/LICENSE"><img src="https://img.shields.io/npm/l/vue-use-date-format.svg" alt="license" /></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install vue-use-date-format
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { useDateFormat } from 'vue-use-date-format';
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Get the formatted date according to the string of tokens passed in, inspired
|
|
29
|
+
by [dayjs](https://github.com/iamkun/dayjs).
|
|
30
|
+
|
|
31
|
+
**List of all available formats (HH:mm:ss by default):**
|
|
32
|
+
|
|
33
|
+
| Format | Output | Description |
|
|
34
|
+
| ------ | ------------------------ | --------------------------------------- |
|
|
35
|
+
| `Yo` | 2018th | Ordinal formatted year |
|
|
36
|
+
| `YY` | 18 | Two-digit year |
|
|
37
|
+
| `YYYY` | 2018 | Four-digit year |
|
|
38
|
+
| `M` | 1-12 | The month, beginning at 1 |
|
|
39
|
+
| `Mo` | 1st, 2nd, ..., 12th | The month, ordinal formatted |
|
|
40
|
+
| `MM` | 01-12 | The month, 2-digits |
|
|
41
|
+
| `MMM` | Jan-Dec | The abbreviated month name |
|
|
42
|
+
| `MMMM` | January-December | The full month name |
|
|
43
|
+
| `D` | 1-31 | The day of the month |
|
|
44
|
+
| `Do` | 1st, 2nd, ..., 31st | The day of the month, ordinal formatted |
|
|
45
|
+
| `DD` | 01-31 | The day of the month, 2-digits |
|
|
46
|
+
| `H` | 0-23 | The hour |
|
|
47
|
+
| `Ho` | 0th, 1st, 2nd, ..., 23rd | The hour, ordinal formatted |
|
|
48
|
+
| `HH` | 00-23 | The hour, 2-digits |
|
|
49
|
+
| `h` | 1-12 | The hour, 12-hour clock |
|
|
50
|
+
| `ho` | 1st, 2nd, ..., 12th | The hour, 12-hour clock, sorted |
|
|
51
|
+
| `hh` | 01-12 | The hour, 12-hour clock, 2-digits |
|
|
52
|
+
| `m` | 0-59 | The minute |
|
|
53
|
+
| `mo` | 0th, 1st, ..., 59th | The minute, ordinal formatted |
|
|
54
|
+
| `mm` | 00-59 | The minute, 2-digits |
|
|
55
|
+
| `s` | 0-59 | The second |
|
|
56
|
+
| `so` | 0th, 1st, ..., 59th | The second, ordinal formatted |
|
|
57
|
+
| `ss` | 00-59 | The second, 2-digits |
|
|
58
|
+
| `SSS` | 000-999 | The millisecond, 3-digits |
|
|
59
|
+
| `A` | AM PM | The meridiem |
|
|
60
|
+
| `AA` | A.M. P.M. | The meridiem, periods |
|
|
61
|
+
| `a` | am pm | The meridiem, lowercase |
|
|
62
|
+
| `aa` | a.m. p.m. | The meridiem, lowercase and periods |
|
|
63
|
+
| `d` | 0-6 | The day of the week, with Sunday as 0 |
|
|
64
|
+
| `dd` | S-S | The min name of the day of the week |
|
|
65
|
+
| `ddd` | Sun-Sat | The short name of the day of the week |
|
|
66
|
+
| `dddd` | Sunday-Saturday | The name of the day of the week |
|
|
67
|
+
| `z` | GMT, GMT+1 | The timezone with offset |
|
|
68
|
+
| `zz` | GMT, GMT+1 | The timezone with offset |
|
|
69
|
+
| `zzz` | GMT, GMT+1 | The timezone with offset |
|
|
70
|
+
| `zzzz` | GMT, GMT+01:00 | The long timezone with offset |
|
|
71
|
+
|
|
72
|
+
- Meridiem is customizable by defining `customMeridiem` in `options`.
|
|
73
|
+
|
|
74
|
+
### Basic
|
|
75
|
+
|
|
76
|
+
```vue
|
|
77
|
+
<script setup lang="ts">
|
|
78
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
79
|
+
|
|
80
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')
|
|
81
|
+
</script>
|
|
82
|
+
|
|
83
|
+
<template>
|
|
84
|
+
<div>{{ formatted }}</div>
|
|
85
|
+
</template>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Use with locales
|
|
89
|
+
|
|
90
|
+
```vue
|
|
91
|
+
<script setup lang="ts">
|
|
92
|
+
import { useDateFormat, useNow } from 'vue-use-date-format'
|
|
93
|
+
|
|
94
|
+
const formatted = useDateFormat(useNow(), 'YYYY-MM-DD (ddd)', { locales: 'en-US' })
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<template>
|
|
98
|
+
<div>{{ formatted }}</div>
|
|
99
|
+
</template>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Use with custom meridiem
|
|
103
|
+
|
|
104
|
+
```vue
|
|
105
|
+
<script setup lang="ts">
|
|
106
|
+
import { useDateFormat } from 'vue-use-date-format'
|
|
107
|
+
|
|
108
|
+
function customMeridiem(hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) {
|
|
109
|
+
const m = hours > 11 ? (isLowercase ? 'μμ' : 'ΜΜ') : (isLowercase ? 'πμ' : 'ΠΜ')
|
|
110
|
+
return hasPeriod ? m.split('').reduce((acc, current) => acc += `${current}.`, '') : m
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const am = useDateFormat('2022-01-01 05:05:05', 'hh:mm:ss A', { customMeridiem })
|
|
114
|
+
// am.value = '05:05:05 ΠΜ'
|
|
115
|
+
const pm = useDateFormat('2022-01-01 17:05:05', 'hh:mm:ss AA', { customMeridiem })
|
|
116
|
+
// pm.value = '05:05:05 Μ.Μ.'
|
|
117
|
+
</script>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> Extracted from [VueUse](https://vueuse.org/) for standalone use.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const vue = require('vue');
|
|
4
|
+
|
|
5
|
+
const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[T\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/i;
|
|
6
|
+
const REGEX_FORMAT = /[YMDHhms]o|\[([^\]]+)\]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a{1,2}|A{1,2}|m{1,2}|s{1,2}|Z{1,2}|z{1,4}|SSS/g;
|
|
7
|
+
function defaultMeridiem(hours, minutes, isLowercase, hasPeriod) {
|
|
8
|
+
let m = hours < 12 ? "AM" : "PM";
|
|
9
|
+
if (hasPeriod)
|
|
10
|
+
m = m.split("").reduce((acc, curr) => acc += `${curr}.`, "");
|
|
11
|
+
return isLowercase ? m.toLowerCase() : m;
|
|
12
|
+
}
|
|
13
|
+
function formatOrdinal(num) {
|
|
14
|
+
const suffixes = ["th", "st", "nd", "rd"];
|
|
15
|
+
const v = num % 100;
|
|
16
|
+
return num + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);
|
|
17
|
+
}
|
|
18
|
+
function formatDate(date, formatStr, options = {}) {
|
|
19
|
+
const years = date.getFullYear();
|
|
20
|
+
const month = date.getMonth();
|
|
21
|
+
const days = date.getDate();
|
|
22
|
+
const hours = date.getHours();
|
|
23
|
+
const minutes = date.getMinutes();
|
|
24
|
+
const seconds = date.getSeconds();
|
|
25
|
+
const milliseconds = date.getMilliseconds();
|
|
26
|
+
const day = date.getDay();
|
|
27
|
+
const meridiem = options.customMeridiem ?? defaultMeridiem;
|
|
28
|
+
const stripTimeZone = (dateString) => {
|
|
29
|
+
return dateString.split(" ")[1] ?? "";
|
|
30
|
+
};
|
|
31
|
+
const matches = {
|
|
32
|
+
Yo: () => formatOrdinal(years),
|
|
33
|
+
YY: () => String(years).slice(-2),
|
|
34
|
+
YYYY: () => years,
|
|
35
|
+
M: () => month + 1,
|
|
36
|
+
Mo: () => formatOrdinal(month + 1),
|
|
37
|
+
MM: () => `${month + 1}`.padStart(2, "0"),
|
|
38
|
+
MMM: () => date.toLocaleDateString(vue.toValue(options.locales), { month: "short" }),
|
|
39
|
+
MMMM: () => date.toLocaleDateString(vue.toValue(options.locales), { month: "long" }),
|
|
40
|
+
D: () => String(days),
|
|
41
|
+
Do: () => formatOrdinal(days),
|
|
42
|
+
DD: () => `${days}`.padStart(2, "0"),
|
|
43
|
+
H: () => String(hours),
|
|
44
|
+
Ho: () => formatOrdinal(hours),
|
|
45
|
+
HH: () => `${hours}`.padStart(2, "0"),
|
|
46
|
+
h: () => `${hours % 12 || 12}`.padStart(1, "0"),
|
|
47
|
+
ho: () => formatOrdinal(hours % 12 || 12),
|
|
48
|
+
hh: () => `${hours % 12 || 12}`.padStart(2, "0"),
|
|
49
|
+
m: () => String(minutes),
|
|
50
|
+
mo: () => formatOrdinal(minutes),
|
|
51
|
+
mm: () => `${minutes}`.padStart(2, "0"),
|
|
52
|
+
s: () => String(seconds),
|
|
53
|
+
so: () => formatOrdinal(seconds),
|
|
54
|
+
ss: () => `${seconds}`.padStart(2, "0"),
|
|
55
|
+
SSS: () => `${milliseconds}`.padStart(3, "0"),
|
|
56
|
+
d: () => day,
|
|
57
|
+
dd: () => date.toLocaleDateString(vue.toValue(options.locales), { weekday: "narrow" }),
|
|
58
|
+
ddd: () => date.toLocaleDateString(vue.toValue(options.locales), { weekday: "short" }),
|
|
59
|
+
dddd: () => date.toLocaleDateString(vue.toValue(options.locales), { weekday: "long" }),
|
|
60
|
+
A: () => meridiem(hours, minutes),
|
|
61
|
+
AA: () => meridiem(hours, minutes, false, true),
|
|
62
|
+
a: () => meridiem(hours, minutes, true),
|
|
63
|
+
aa: () => meridiem(hours, minutes, true, true),
|
|
64
|
+
z: () => stripTimeZone(date.toLocaleDateString(vue.toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
65
|
+
zz: () => stripTimeZone(date.toLocaleDateString(vue.toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
66
|
+
zzz: () => stripTimeZone(date.toLocaleDateString(vue.toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
67
|
+
zzzz: () => stripTimeZone(date.toLocaleDateString(vue.toValue(options.locales), { timeZoneName: "longOffset" }))
|
|
68
|
+
};
|
|
69
|
+
return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 ?? matches[match]?.() ?? match);
|
|
70
|
+
}
|
|
71
|
+
function normalizeDate(date) {
|
|
72
|
+
if (date === null)
|
|
73
|
+
return new Date(Number.NaN);
|
|
74
|
+
if (date === void 0)
|
|
75
|
+
return /* @__PURE__ */ new Date();
|
|
76
|
+
if (date instanceof Date)
|
|
77
|
+
return new Date(date);
|
|
78
|
+
if (typeof date === "string" && !/Z$/i.test(date)) {
|
|
79
|
+
const d = date.match(REGEX_PARSE);
|
|
80
|
+
if (d) {
|
|
81
|
+
const m = d[2] - 1 || 0;
|
|
82
|
+
const ms = (d[7] || "0").substring(0, 3);
|
|
83
|
+
return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return new Date(date);
|
|
87
|
+
}
|
|
88
|
+
// @__NO_SIDE_EFFECTS__
|
|
89
|
+
function useDateFormat(date, formatStr = "HH:mm:ss", options = {}) {
|
|
90
|
+
return vue.computed(() => formatDate(normalizeDate(vue.toValue(date)), vue.toValue(formatStr), options));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
exports.formatDate = formatDate;
|
|
94
|
+
exports.normalizeDate = normalizeDate;
|
|
95
|
+
exports.useDateFormat = useDateFormat;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MaybeRefOrGetter, ComputedRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type DateLike = Date | number | string | undefined;
|
|
4
|
+
interface UseDateFormatOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The locale(s) to used for dd/ddd/dddd/MMM/MMMM format
|
|
7
|
+
*
|
|
8
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
|
|
9
|
+
*/
|
|
10
|
+
locales?: MaybeRefOrGetter<Intl.LocalesArgument>;
|
|
11
|
+
/**
|
|
12
|
+
* A custom function to re-modify the way to display meridiem
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
customMeridiem?: (hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) => string;
|
|
16
|
+
}
|
|
17
|
+
declare function formatDate(date: Date, formatStr: string, options?: UseDateFormatOptions): string;
|
|
18
|
+
declare function normalizeDate(date: DateLike): Date;
|
|
19
|
+
type UseDateFormatReturn = ComputedRef<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
22
|
+
*
|
|
23
|
+
* @see https://vueuse.org/useDateFormat
|
|
24
|
+
* @param date - The date to format, can either be a `Date` object, a timestamp, or a string
|
|
25
|
+
* @param formatStr - The combination of tokens to format the date
|
|
26
|
+
* @param options - UseDateFormatOptions
|
|
27
|
+
*
|
|
28
|
+
* @__NO_SIDE_EFFECTS__
|
|
29
|
+
*/
|
|
30
|
+
declare function useDateFormat(date: MaybeRefOrGetter<DateLike>, formatStr?: MaybeRefOrGetter<string>, options?: UseDateFormatOptions): UseDateFormatReturn;
|
|
31
|
+
|
|
32
|
+
export { formatDate, normalizeDate, useDateFormat };
|
|
33
|
+
export type { DateLike, UseDateFormatOptions, UseDateFormatReturn };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MaybeRefOrGetter, ComputedRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type DateLike = Date | number | string | undefined;
|
|
4
|
+
interface UseDateFormatOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The locale(s) to used for dd/ddd/dddd/MMM/MMMM format
|
|
7
|
+
*
|
|
8
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
|
|
9
|
+
*/
|
|
10
|
+
locales?: MaybeRefOrGetter<Intl.LocalesArgument>;
|
|
11
|
+
/**
|
|
12
|
+
* A custom function to re-modify the way to display meridiem
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
customMeridiem?: (hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) => string;
|
|
16
|
+
}
|
|
17
|
+
declare function formatDate(date: Date, formatStr: string, options?: UseDateFormatOptions): string;
|
|
18
|
+
declare function normalizeDate(date: DateLike): Date;
|
|
19
|
+
type UseDateFormatReturn = ComputedRef<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
22
|
+
*
|
|
23
|
+
* @see https://vueuse.org/useDateFormat
|
|
24
|
+
* @param date - The date to format, can either be a `Date` object, a timestamp, or a string
|
|
25
|
+
* @param formatStr - The combination of tokens to format the date
|
|
26
|
+
* @param options - UseDateFormatOptions
|
|
27
|
+
*
|
|
28
|
+
* @__NO_SIDE_EFFECTS__
|
|
29
|
+
*/
|
|
30
|
+
declare function useDateFormat(date: MaybeRefOrGetter<DateLike>, formatStr?: MaybeRefOrGetter<string>, options?: UseDateFormatOptions): UseDateFormatReturn;
|
|
31
|
+
|
|
32
|
+
export { formatDate, normalizeDate, useDateFormat };
|
|
33
|
+
export type { DateLike, UseDateFormatOptions, UseDateFormatReturn };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { MaybeRefOrGetter, ComputedRef } from 'vue';
|
|
2
|
+
|
|
3
|
+
type DateLike = Date | number | string | undefined;
|
|
4
|
+
interface UseDateFormatOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The locale(s) to used for dd/ddd/dddd/MMM/MMMM format
|
|
7
|
+
*
|
|
8
|
+
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
|
|
9
|
+
*/
|
|
10
|
+
locales?: MaybeRefOrGetter<Intl.LocalesArgument>;
|
|
11
|
+
/**
|
|
12
|
+
* A custom function to re-modify the way to display meridiem
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
customMeridiem?: (hours: number, minutes: number, isLowercase?: boolean, hasPeriod?: boolean) => string;
|
|
16
|
+
}
|
|
17
|
+
declare function formatDate(date: Date, formatStr: string, options?: UseDateFormatOptions): string;
|
|
18
|
+
declare function normalizeDate(date: DateLike): Date;
|
|
19
|
+
type UseDateFormatReturn = ComputedRef<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Get the formatted date according to the string of tokens passed in.
|
|
22
|
+
*
|
|
23
|
+
* @see https://vueuse.org/useDateFormat
|
|
24
|
+
* @param date - The date to format, can either be a `Date` object, a timestamp, or a string
|
|
25
|
+
* @param formatStr - The combination of tokens to format the date
|
|
26
|
+
* @param options - UseDateFormatOptions
|
|
27
|
+
*
|
|
28
|
+
* @__NO_SIDE_EFFECTS__
|
|
29
|
+
*/
|
|
30
|
+
declare function useDateFormat(date: MaybeRefOrGetter<DateLike>, formatStr?: MaybeRefOrGetter<string>, options?: UseDateFormatOptions): UseDateFormatReturn;
|
|
31
|
+
|
|
32
|
+
export { formatDate, normalizeDate, useDateFormat };
|
|
33
|
+
export type { DateLike, UseDateFormatOptions, UseDateFormatReturn };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { computed, toValue } from 'vue';
|
|
2
|
+
|
|
3
|
+
const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[T\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/i;
|
|
4
|
+
const REGEX_FORMAT = /[YMDHhms]o|\[([^\]]+)\]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a{1,2}|A{1,2}|m{1,2}|s{1,2}|Z{1,2}|z{1,4}|SSS/g;
|
|
5
|
+
function defaultMeridiem(hours, minutes, isLowercase, hasPeriod) {
|
|
6
|
+
let m = hours < 12 ? "AM" : "PM";
|
|
7
|
+
if (hasPeriod)
|
|
8
|
+
m = m.split("").reduce((acc, curr) => acc += `${curr}.`, "");
|
|
9
|
+
return isLowercase ? m.toLowerCase() : m;
|
|
10
|
+
}
|
|
11
|
+
function formatOrdinal(num) {
|
|
12
|
+
const suffixes = ["th", "st", "nd", "rd"];
|
|
13
|
+
const v = num % 100;
|
|
14
|
+
return num + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]);
|
|
15
|
+
}
|
|
16
|
+
function formatDate(date, formatStr, options = {}) {
|
|
17
|
+
const years = date.getFullYear();
|
|
18
|
+
const month = date.getMonth();
|
|
19
|
+
const days = date.getDate();
|
|
20
|
+
const hours = date.getHours();
|
|
21
|
+
const minutes = date.getMinutes();
|
|
22
|
+
const seconds = date.getSeconds();
|
|
23
|
+
const milliseconds = date.getMilliseconds();
|
|
24
|
+
const day = date.getDay();
|
|
25
|
+
const meridiem = options.customMeridiem ?? defaultMeridiem;
|
|
26
|
+
const stripTimeZone = (dateString) => {
|
|
27
|
+
return dateString.split(" ")[1] ?? "";
|
|
28
|
+
};
|
|
29
|
+
const matches = {
|
|
30
|
+
Yo: () => formatOrdinal(years),
|
|
31
|
+
YY: () => String(years).slice(-2),
|
|
32
|
+
YYYY: () => years,
|
|
33
|
+
M: () => month + 1,
|
|
34
|
+
Mo: () => formatOrdinal(month + 1),
|
|
35
|
+
MM: () => `${month + 1}`.padStart(2, "0"),
|
|
36
|
+
MMM: () => date.toLocaleDateString(toValue(options.locales), { month: "short" }),
|
|
37
|
+
MMMM: () => date.toLocaleDateString(toValue(options.locales), { month: "long" }),
|
|
38
|
+
D: () => String(days),
|
|
39
|
+
Do: () => formatOrdinal(days),
|
|
40
|
+
DD: () => `${days}`.padStart(2, "0"),
|
|
41
|
+
H: () => String(hours),
|
|
42
|
+
Ho: () => formatOrdinal(hours),
|
|
43
|
+
HH: () => `${hours}`.padStart(2, "0"),
|
|
44
|
+
h: () => `${hours % 12 || 12}`.padStart(1, "0"),
|
|
45
|
+
ho: () => formatOrdinal(hours % 12 || 12),
|
|
46
|
+
hh: () => `${hours % 12 || 12}`.padStart(2, "0"),
|
|
47
|
+
m: () => String(minutes),
|
|
48
|
+
mo: () => formatOrdinal(minutes),
|
|
49
|
+
mm: () => `${minutes}`.padStart(2, "0"),
|
|
50
|
+
s: () => String(seconds),
|
|
51
|
+
so: () => formatOrdinal(seconds),
|
|
52
|
+
ss: () => `${seconds}`.padStart(2, "0"),
|
|
53
|
+
SSS: () => `${milliseconds}`.padStart(3, "0"),
|
|
54
|
+
d: () => day,
|
|
55
|
+
dd: () => date.toLocaleDateString(toValue(options.locales), { weekday: "narrow" }),
|
|
56
|
+
ddd: () => date.toLocaleDateString(toValue(options.locales), { weekday: "short" }),
|
|
57
|
+
dddd: () => date.toLocaleDateString(toValue(options.locales), { weekday: "long" }),
|
|
58
|
+
A: () => meridiem(hours, minutes),
|
|
59
|
+
AA: () => meridiem(hours, minutes, false, true),
|
|
60
|
+
a: () => meridiem(hours, minutes, true),
|
|
61
|
+
aa: () => meridiem(hours, minutes, true, true),
|
|
62
|
+
z: () => stripTimeZone(date.toLocaleDateString(toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
63
|
+
zz: () => stripTimeZone(date.toLocaleDateString(toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
64
|
+
zzz: () => stripTimeZone(date.toLocaleDateString(toValue(options.locales), { timeZoneName: "shortOffset" })),
|
|
65
|
+
zzzz: () => stripTimeZone(date.toLocaleDateString(toValue(options.locales), { timeZoneName: "longOffset" }))
|
|
66
|
+
};
|
|
67
|
+
return formatStr.replace(REGEX_FORMAT, (match, $1) => $1 ?? matches[match]?.() ?? match);
|
|
68
|
+
}
|
|
69
|
+
function normalizeDate(date) {
|
|
70
|
+
if (date === null)
|
|
71
|
+
return new Date(Number.NaN);
|
|
72
|
+
if (date === void 0)
|
|
73
|
+
return /* @__PURE__ */ new Date();
|
|
74
|
+
if (date instanceof Date)
|
|
75
|
+
return new Date(date);
|
|
76
|
+
if (typeof date === "string" && !/Z$/i.test(date)) {
|
|
77
|
+
const d = date.match(REGEX_PARSE);
|
|
78
|
+
if (d) {
|
|
79
|
+
const m = d[2] - 1 || 0;
|
|
80
|
+
const ms = (d[7] || "0").substring(0, 3);
|
|
81
|
+
return new Date(d[1], m, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return new Date(date);
|
|
85
|
+
}
|
|
86
|
+
// @__NO_SIDE_EFFECTS__
|
|
87
|
+
function useDateFormat(date, formatStr = "HH:mm:ss", options = {}) {
|
|
88
|
+
return computed(() => formatDate(normalizeDate(toValue(date)), toValue(formatStr), options));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { formatDate, normalizeDate, useDateFormat };
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vue-use-date-format",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Vue 3 reactive date formatting with customizable tokens",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "unbuild",
|
|
20
|
+
"prepublishOnly": "cp README.npm.md README.md && npm run build",
|
|
21
|
+
"postpublish": "cp README.github.md README.md"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"vue",
|
|
25
|
+
"vue3",
|
|
26
|
+
"vue-3",
|
|
27
|
+
"composable",
|
|
28
|
+
"composition-api",
|
|
29
|
+
"vueuse",
|
|
30
|
+
"time",
|
|
31
|
+
"date",
|
|
32
|
+
"datetime",
|
|
33
|
+
"timer",
|
|
34
|
+
"use",
|
|
35
|
+
"format",
|
|
36
|
+
"reactive",
|
|
37
|
+
"tokens",
|
|
38
|
+
"strftime",
|
|
39
|
+
"localize",
|
|
40
|
+
"useDateFormat"
|
|
41
|
+
],
|
|
42
|
+
"author": "VueFrag",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/vuefrag/vue-use-date-format.git"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/vuefrag/vue-use-date-format/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/vuefrag/vue-use-date-format#readme",
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"vue": ">=3.0.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"unbuild": "^2.0.0",
|
|
57
|
+
"typescript": "^5.0.0",
|
|
58
|
+
"vue": "^3.4.0"
|
|
59
|
+
}
|
|
60
|
+
}
|