Skip to content

Commit 0275358

Browse files
feat: addHour, addMinute, addSecond
1 parent 59db3ef commit 0275358

File tree

11 files changed

+355
-4
lines changed

11 files changed

+355
-4
lines changed

Diff for: docs/app.vue

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script setup lang="ts"></script>
22

33
<template>
4-
<TheHeader />
4+
<!-- <TheHeader /> -->
55
<ContainerSection>
6-
<TheSearch />
6+
<!-- <TheSearch /> -->
77
<main class="mt-20">
8+
<ContentModify />
89
<ContentIntroduction />
910
<ContentInstallation />
1011
<ContentFormat />

Diff for: docs/components/FunctionReference.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const props = defineProps<{
1616
>: <span class="text-sky-500">{{ arg.type }}</span
1717
>{{ index < props.arguments.length - 1 ? ", " : ""
1818
}}<span v-if="arg.comment" class="text-gray-400"
19-
>// {{ arg.comment }}</span
19+
>&nbsp;// {{ arg.comment }}</span
2020
>
2121
</div> </template
2222
>): <span class="text-sky-500">{{ props.return }}</span>

Diff for: docs/components/content/Modify.vue

+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<script lang="ts" setup>
2+
const fns = {
3+
addDay: {
4+
description:
5+
"Returns a new Date object with a positive or negative number of days applied to date argument. To subtract days, use a negative number.",
6+
return: "Date",
7+
arguments: [
8+
{
9+
name: "date",
10+
type: "string | Date",
11+
},
12+
{
13+
name: "amount",
14+
type: "number",
15+
},
16+
],
17+
},
18+
addHour: {
19+
description:
20+
"Returns a new Date object with a positive or negative number of hours applied to date argument. To subtract hours, use a negative number.",
21+
return: "Date",
22+
arguments: [
23+
{
24+
name: "date",
25+
type: "string | Date",
26+
},
27+
{
28+
name: "amount",
29+
type: "number",
30+
},
31+
],
32+
},
33+
addMinute: {
34+
description:
35+
"Returns a new Date object with a positive or negative number of minutes applied to date argument. To subtract minutes, use a negative number.",
36+
return: "Date",
37+
arguments: [
38+
{
39+
name: "date",
40+
type: "string | Date",
41+
},
42+
{
43+
name: "amount",
44+
type: "number",
45+
},
46+
],
47+
},
48+
addMonth: {
49+
description: `Returns a new Date object with a positive or negative number of
50+
months applied to date argument. To subtract months, use a negative number.
51+
Sometimes the result will "overflow" the available days of
52+
the result month. For example when adding 1 month to January 31st the
53+
resulting date would be February 31st, which does not exist. By default, the
54+
date will be set to the last day of February but you could opt for it
55+
to "overflow" into March by setting <code>dateOverflow</code> to
56+
<code>true</code>.`,
57+
return: "Date",
58+
arguments: [
59+
{
60+
name: "date",
61+
type: "string | Date",
62+
},
63+
{
64+
name: "amount",
65+
type: "number",
66+
},
67+
{
68+
name: "dateOverflow",
69+
type: "boolean",
70+
},
71+
],
72+
},
73+
addSecond: {
74+
description:
75+
"Returns a new Date object with a positive or negative number of seconds applied to date argument. To subtract seconds, use a negative number.",
76+
return: "Date",
77+
arguments: [
78+
{
79+
name: "date",
80+
type: "string | Date",
81+
},
82+
{
83+
name: "amount",
84+
type: "number",
85+
},
86+
],
87+
},
88+
addYear: {
89+
description: `Returns a new Date object with a positive or negative number of years
90+
applied to date argument. To subtract years, use a negative number.
91+
Sometimes the result will "overflow" the available days of
92+
the result month. For example when adding 1 year to February 29, 2024 the
93+
resulting date would be February 29, 2025, which does not exist (2025 is
94+
not a leap year). By default, the date will be set to February 28, 2025 but
95+
you could opt for it to "overflow" into March by setting
96+
<code>dateOverflow</code> to <code>true</code>.`,
97+
return: "Date",
98+
arguments: [
99+
{
100+
name: "date",
101+
type: "string | Date",
102+
},
103+
{
104+
name: "amount",
105+
type: "number",
106+
},
107+
{
108+
name: "dateOverflow",
109+
type: "boolean",
110+
},
111+
],
112+
},
113+
applyOffset: {
114+
description: `Returns a new Date object with a timezone offset applied to date argument
115+
— this function does fundamentally change the date but can be very useful
116+
when working with timezones. Read more in the timezone section.`,
117+
return: "Date",
118+
arguments: [
119+
{
120+
name: "date",
121+
type: "string | Date",
122+
},
123+
{
124+
name: "offset",
125+
type: "string",
126+
comment: "+-HHmm, ex: -0800",
127+
},
128+
],
129+
},
130+
dayStart: {
131+
description: `Returns a new Date object with the time set to 00:00:00.000 (local time).`,
132+
return: "Date",
133+
arguments: [
134+
{
135+
name: "date",
136+
type: "string | Date",
137+
},
138+
],
139+
},
140+
dayEnd: {
141+
description: `Returns a new Date object with the time set to 23:59:59 (local).`,
142+
return: "Date",
143+
arguments: [
144+
{
145+
name: "date",
146+
type: "string | Date",
147+
},
148+
],
149+
},
150+
monthEnd: {
151+
description: `Returns a new Date object with the date set to the last day of the current month (does not modify the time).`,
152+
return: "Date",
153+
arguments: [
154+
{
155+
name: "date",
156+
type: "string | Date",
157+
},
158+
],
159+
},
160+
monthStart: {
161+
description: `Returns a new Date object with the date set to the first day of the current month and the time set to 00:00:00 (local).`,
162+
return: "Date",
163+
arguments: [
164+
{
165+
name: "date",
166+
type: "string | Date",
167+
},
168+
],
169+
},
170+
removeOffset: {
171+
description: `Returns a new Date object with the inverse of the specified timezone offset removed. This can be helpful to normalize time information across timezones.`,
172+
return: "Date",
173+
arguments: [
174+
{
175+
name: "date",
176+
type: "string | Date",
177+
},
178+
{
179+
name: "offset",
180+
type: "string",
181+
comment: "+-HHmm, ex: -0800",
182+
},
183+
],
184+
},
185+
weekStart: {
186+
description: `Returns a new Date object with the date set to the first day of the current week with the time set to 00:00:00 (local).`,
187+
return: "Date",
188+
arguments: [
189+
{
190+
name: "date",
191+
type: "string | Date",
192+
},
193+
{
194+
name: "startOfWeekDay",
195+
type: "number",
196+
comment: "0-6, 0 is Sunday",
197+
},
198+
],
199+
},
200+
weekEnd: {
201+
description: `Returns a new Date object with the date set to the last day of the current week with the time set to 23:59:59 (local).`,
202+
return: "Date",
203+
arguments: [
204+
{
205+
name: "date",
206+
type: "string | Date",
207+
},
208+
{
209+
name: "startOfWeekDay",
210+
type: "number",
211+
comment: "0-6, 0 is Sunday",
212+
},
213+
],
214+
},
215+
}
216+
</script>
217+
218+
<template>
219+
<PageSection id="modify">
220+
<HeadingSection title="Modify" class="text-sky-500" />
221+
<p>
222+
Tempo includes a number of (tree-shakable) utility functions to assist you
223+
in your date modifying needs. These functions all accept either an ISO
224+
8601 string or a Date object and return a <em>new Date object</em> (they
225+
do not change the date argument).
226+
</p>
227+
<div v-for="(def, fn) in fns">
228+
<h4>{{ fn }}</h4>
229+
<p v-html="def.description" />
230+
<FunctionReference
231+
:function="fn"
232+
:arguments="def.arguments"
233+
:return="def.return"
234+
/>
235+
</div>
236+
</PageSection>
237+
</template>

Diff for: docs/components/content/Parse.vue

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,21 @@ const parseOptionsProperties = [
8282
familiar, but what is <code>partFilter</code> and
8383
<code>dataOverflow</code>?
8484
</p>
85-
<h4><code>partFilter</code></h4>
85+
<h4>partFilter</h4>
8686
<p>
8787
The <code>partFilter</code> option gives you fine grained control over
8888
which pieces and parts of a date you’d like to include in the final parsed
8989
date (remember, missing "parts" will default to the current date at
9090
midnight local).
9191
</p>
9292
<CodeExample file="part-filter" />
93+
<h4>dateOverflow</h4>
94+
<p>
95+
The <code>dateOverflow</code> option determines how an “out of range” date
96+
should be parsed. Options are <code>backward</code> (default),
97+
<code>forward</code>, <code>throw</code>.
98+
</p>
99+
<CodeExample file="date-overflow" />
100+
<CodeExample file="date-overflow-throw" />
93101
</PageSection>
94102
</template>

Diff for: docs/examples/date-overflow-throw.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { parse } from "@formkit/tempo"
2+
3+
// Nov 31 does not exist!
4+
const date = "2011-11-31"
5+
6+
// Overflows can throw an error
7+
parse({
8+
date,
9+
format: "YYYY-MM-DD",
10+
locale: "en-US",
11+
dateOverflow: "throw",
12+
})

Diff for: docs/examples/date-overflow.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { parse } from "@formkit/tempo"
2+
3+
// Nov 31 does not exist!
4+
const date = "2011-11-31"
5+
6+
// Default overflow is "backward"
7+
parse(date, "YYYY-MM-DD", "en-US")
8+
9+
// Parse "forward" into December
10+
parse({
11+
date,
12+
format: "YYYY-MM-DD",
13+
locale: "en-US",
14+
dateOverflow: "forward",
15+
})

Diff for: src/__tests__/tempo.spec.ts

+42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
addDay,
1111
addYear,
1212
addMonth,
13+
addMinute,
14+
addSecond,
15+
addHour,
1316
format,
1417
formatStr,
1518
sameDay,
@@ -233,6 +236,45 @@ describe("addDay", () => {
233236
})
234237
})
235238

239+
describe("addHour", () => {
240+
it("can increment a normal hour", () => {
241+
expect(addHour("2022-01-01T00:00:00Z").toISOString()).toBe(
242+
"2022-01-01T01:00:00.000Z"
243+
)
244+
})
245+
it("can increment the last hours of the day into a new day", () => {
246+
expect(addHour("2022-01-01T23:11:00Z", 3).toISOString()).toBe(
247+
"2022-01-02T02:11:00.000Z"
248+
)
249+
})
250+
})
251+
252+
describe("addMinute", () => {
253+
it("can increment a normal hour", () => {
254+
expect(addMinute("2022-01-01T00:00:00Z").toISOString()).toBe(
255+
"2022-01-01T00:01:00.000Z"
256+
)
257+
})
258+
it("can increment the last hours of the day into a new day", () => {
259+
expect(addMinute("2022-01-01T23:11:00Z", 181).toISOString()).toBe(
260+
"2022-01-02T02:12:00.000Z"
261+
)
262+
})
263+
})
264+
265+
describe("addSecond", () => {
266+
it("can increment a normal hour", () => {
267+
expect(addSecond("2022-01-01T00:00:00Z").toISOString()).toBe(
268+
"2022-01-01T00:00:01.000Z"
269+
)
270+
})
271+
it("can increment the last hours of the day into a new day", () => {
272+
expect(addSecond("2022-01-01T23:11:00Z", 3600 * 3 + 1).toISOString()).toBe(
273+
"2022-01-02T02:11:01.000Z"
274+
)
275+
})
276+
})
277+
236278
describe("addMonth", () => {
237279
it("gets the next month on the first", () => {
238280
expect(addMonth("2022-01-01").toISOString()).toBe(

Diff for: src/addHour.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { date } from "./date"
2+
3+
/**
4+
* Returns a new date object 1/n hours after the original one.
5+
* @param inputDate - A date to increment by 1 day.
6+
*/
7+
export function addHour(inputDate: DateInput, count = 1) {
8+
const d = date(inputDate)
9+
d.setHours(d.getHours() + count)
10+
return d
11+
}

Diff for: src/addMinute.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { date } from "./date"
2+
3+
/**
4+
* Returns a new date object 1/n seconds after the original one.
5+
* @param inputDate - A date to increment by 1 day.
6+
*/
7+
export function addMinute(inputDate: DateInput, count = 1) {
8+
const d = date(inputDate)
9+
d.setMinutes(d.getMinutes() + count)
10+
return d
11+
}

0 commit comments

Comments
 (0)