1313* Proper type definitions
1414* More correct code
1515* ESM-only
16+ * Additional [ utilities] ( #utils )
1617
1718## API
1819
19- tinytime exports a single function that returns a template object. This object has a single method, ` render ` , which
20+ Tinytime exports a single function that returns a template object. This object has a single method, ` render ` , which
2021takes a ` Date ` and returns a string with the rendered data.
2122
2223``` js
@@ -50,12 +51,66 @@ template.render(new Date());
5051> const template = tinytime (' {Mo}' , { padMonth: true })
5152> ` ` `
5253
54+ ## Utils
55+
56+ Tinytime now comes with two utility functions exported from ` tinytime/ utils`
57+
58+ ### Date parsing
59+
60+ The ` parseExact (string, string)` function can be used to easily parse a date string according to a specified format.
61+
62+ ` ` ` ts
63+ import { parseExact } from " tinytime/parseExact" ;
64+
65+ const date = parseExact (" 05 21 1997 (at 06:37:00 PM)" , " MM dd yyyy (at hh:mm:ss aa)" );
66+
67+ console .assert (date === new Date (" 1997-05-21T18:37:00Z" )); // assertion passes
68+ ```
69+
70+ The format tokens are as follows:
71+
72+ * ` y ` - year
73+ * ` M ` - month
74+ * ` d ` - date
75+ * ` h ` - hours
76+ * ` m ` - minutes
77+ * ` s ` - seconds
78+ * ` i ` - milliseconds
79+ * ` a ` - AM/PM
80+
81+ ### Date manipulation
82+
83+ The ` addToDate(Date, DateDelta) ` function can be used to add a desired delta to a given date.
84+
85+ ``` ts
86+ import { addToDate } from " tinytime/addToDate"
87+
88+ const now = Date .now (); // 2025-05-17T15:08:00.000Z
89+
90+ const future = addToDate (now , { days: 3 , years: 100 , hours: 761 });
91+
92+ future .toISOString (); // 2125-06-21T08:08:00.000Z
93+ ```
94+
95+ The ` DateDelta ` is defined as follows:
96+
97+ ``` ts
98+ interface DateDelta {
99+ years? : number ;
100+ months? : number ;
101+ days? : number ;
102+ hours? : number ;
103+ minutes? : number ;
104+ seconds? : number ;
105+ milliseconds? : number ;
106+ }
107+ ```
53108
54109## Efficiency
55110
56- tinytime takes an approach similar to a compiler and generates an AST representing your template. This AST is generated when
57- you call the main ` tinytime` function. This lets you efficiently re-render your template without tinytime having to parse the
58- template string again. That means its important that you aren't recreating the template object frequently.
111+ Tinytime takes an approach similar to a compiler and generates an AST representing your template. This AST is generated when
112+ you call the main ` tinytime ` function. This lets you efficiently re-render your template without Tinytime having to parse the
113+ template string again. That means it's important that you aren't recreating the template object frequently.
59114
60115Here's an example showing the right and wrong way to use tinytime with React.
61116
@@ -71,7 +126,7 @@ function Time({ date }) {
71126}
72127```
73128
74- Instead, only create the template object once, and just re-render it.
129+ Instead, only create the template object once and re-render it.
75130
76131``` jsx
77132const template = tinytime (' {h}:{mm}:{ss}{a}' );
@@ -84,6 +139,29 @@ function Time({ date }) {
84139}
85140```
86141
142+ ## Current bundle size
143+
144+ ``` x-sizes
145+ dist\addToDate.js 402 bytes
146+ dist\addToDate.js 246 bytes (GZIP)
147+ dist\addToDate.js 228 bytes (DEFLATE)
148+ dist\addToDate.min.js 270 bytes
149+ dist\addToDate.min.js 195 bytes (GZIP)
150+ dist\addToDate.min.js 177 bytes (DEFLATE)
151+ dist\parseExact.js 1083 bytes
152+ dist\parseExact.js 563 bytes (GZIP)
153+ dist\parseExact.js 545 bytes (DEFLATE)
154+ dist\parseExact.min.js 636 bytes
155+ dist\parseExact.min.js 450 bytes (GZIP)
156+ dist\parseExact.min.js 432 bytes (DEFLATE)
157+ dist\tinytime.js 3674 bytes
158+ dist\tinytime.js 1252 bytes (GZIP)
159+ dist\tinytime.js 1234 bytes (DEFLATE)
160+ dist\tinytime.min.js 1278 bytes
161+ dist\tinytime.min.js 771 bytes (GZIP)
162+ dist\tinytime.min.js 753 bytes (DEFLATE)
163+ ```
164+
87165### Babel Plugins
88166
89167Using one of the plugins below, you can resolve this efficiency concern at compile time.
0 commit comments