small date utilities
0

Configure Feed

Select the types of activity you want to include in your feed.

initial commit

author
Mary
date (Jan 21, 2025, 10:24 PM +0700) commit eeb8c8d3
+265
+4
.vscode/settings.json
··· 1 + { 2 + "editor.defaultFormatter": "denoland.vscode-deno", 3 + "deno.enable": true 4 + }
+17
LICENSE
··· 1 + Permission is hereby granted, free of charge, to any person obtaining a copy 2 + of this software and associated documentation files (the "Software"), to deal 3 + in the Software without restriction, including without limitation the rights 4 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 5 + copies of the Software, and to permit persons to whom the Software is 6 + furnished to do so, subject to the following conditions: 7 + 8 + The above copyright notice and this permission notice shall be included in all 9 + copies or substantial portions of the Software. 10 + 11 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 17 + SOFTWARE.
+3
README.md
··· 1 + # date-fns 2 + 3 + small date utilities
+15
deno.json
··· 1 + { 2 + "name": "@mary/date-fns", 3 + "version": "0.1.0", 4 + "exports": "./lib/mod.ts", 5 + "fmt": { 6 + "useTabs": true, 7 + "indentWidth": 2, 8 + "lineWidth": 110, 9 + "semiColons": true, 10 + "singleQuote": true 11 + }, 12 + "publish": { 13 + "include": ["lib/", "LICENSE", "README.md", "deno.json"] 14 + } 15 + }
+226
lib/mod.ts
··· 1 + export type DayOfWeek = 0 | 1 | 2 | 3 | 4 | 5 | 6; 2 + 3 + export const getUnixMs = (date: Date): number => { 4 + return date.getTime(); 5 + }; 6 + 7 + export const getMilliseconds = (date: Date): number => { 8 + return date.getMilliseconds(); 9 + }; 10 + 11 + export const getSeconds = (date: Date): number => { 12 + return date.getSeconds(); 13 + }; 14 + 15 + export const getMinutes = (date: Date): number => { 16 + return date.getMinutes(); 17 + }; 18 + 19 + export const getHours = (date: Date): number => { 20 + return date.getHours(); 21 + }; 22 + 23 + export const getDayOfWeek = (date: Date): DayOfWeek => { 24 + return date.getDay() as DayOfWeek; 25 + }; 26 + 27 + export const getDayOfMonth = (date: Date): number => { 28 + return date.getDate(); 29 + }; 30 + 31 + export const getMonth = (date: Date): number => { 32 + return date.getMonth(); 33 + }; 34 + 35 + export const getYear = (date: Date): number => { 36 + return date.getFullYear(); 37 + }; 38 + 39 + export const isDatetimeBefore = (date: Date, compare: Date): boolean => { 40 + return date !== compare && getUnixMs(date) < getUnixMs(compare); 41 + }; 42 + 43 + export const isDatetimeAfter = (date: Date, compare: Date): boolean => { 44 + return date !== compare && getUnixMs(date) > getUnixMs(compare); 45 + }; 46 + 47 + export const isDatetimeSame = (a: Date, b: Date): boolean => { 48 + return a === b || getUnixMs(a) === getUnixMs(b); 49 + }; 50 + 51 + export const isDateSame = (a: Date, b: Date): boolean => { 52 + if (a === b) { 53 + return true; 54 + } 55 + 56 + return getDayOfMonth(a) === getDayOfMonth(b) && getMonth(a) === getMonth(b) && getYear(a) === getYear(b); 57 + }; 58 + 59 + export const isMonthSame = (a: Date, b: Date): boolean => { 60 + if (a === b) { 61 + return true; 62 + } 63 + 64 + return getMonth(a) === getMonth(b) && getYear(a) === getYear(b); 65 + }; 66 + 67 + export const isYearSame = (a: Date, b: Date): boolean => { 68 + if (a === b) { 69 + return true; 70 + } 71 + 72 + return getYear(a) === getYear(b); 73 + }; 74 + 75 + export const min = (a: Date, b: Date): Date => { 76 + return isDatetimeBefore(a, b) ? a : b; 77 + }; 78 + 79 + export const max = (a: Date, b: Date): Date => { 80 + return isDatetimeAfter(a, b) ? a : b; 81 + }; 82 + 83 + export const clamp = (date: Date, min: Date | undefined, max: Date | undefined): Date => { 84 + if (min !== undefined && isDatetimeBefore(date, min)) { 85 + return min; 86 + } 87 + 88 + if (max !== undefined && isDatetimeAfter(date, max)) { 89 + return max; 90 + } 91 + 92 + return date; 93 + }; 94 + 95 + export const cloneDate = (date: Date): Date => { 96 + return new Date(date); 97 + }; 98 + 99 + export const startOfDay = (date: Date): Date => { 100 + const d = cloneDate(date); 101 + 102 + d.setHours(0, 0, 0, 0); 103 + return d; 104 + }; 105 + 106 + export const endOfDay = (date: Date): Date => { 107 + const d = cloneDate(date); 108 + 109 + d.setHours(23, 59, 59, 999); 110 + return d; 111 + }; 112 + 113 + export const startOfWeek = (date: Date): Date => { 114 + const d = cloneDate(date); 115 + 116 + d.setDate(getDayOfMonth(d) - getDayOfWeek(d)); 117 + d.setHours(0, 0, 0, 0); 118 + return d; 119 + }; 120 + 121 + export const endOfWeek = (date: Date): Date => { 122 + const d = cloneDate(date); 123 + 124 + d.setDate(getDayOfMonth(d) + (6 - getDayOfWeek(d))); 125 + d.setHours(23, 59, 59, 999); 126 + return d; 127 + }; 128 + 129 + export const startOfMonth = (date: Date): Date => { 130 + const d = cloneDate(date); 131 + 132 + d.setDate(1); 133 + d.setHours(0, 0, 0, 0); 134 + return d; 135 + }; 136 + 137 + export const endOfMonth = (date: Date): Date => { 138 + const d = cloneDate(date); 139 + 140 + d.setMonth(getMonth(d) + 1, 0); 141 + d.setHours(23, 59, 59, 999); 142 + return d; 143 + }; 144 + 145 + export const startOfYear = (date: Date): Date => { 146 + const d = cloneDate(date); 147 + 148 + d.setMonth(0, 1); 149 + d.setHours(0, 0, 0, 0); 150 + return d; 151 + }; 152 + 153 + export const endOfYear = (date: Date): Date => { 154 + const d = cloneDate(date); 155 + 156 + d.setMonth(11, 31); 157 + d.setHours(23, 59, 59, 999); 158 + return d; 159 + }; 160 + 161 + export const addMilliseconds = (date: Date, milliseconds: number): Date => { 162 + const d = cloneDate(date); 163 + 164 + d.setMilliseconds(getMilliseconds(d) + milliseconds); 165 + return d; 166 + }; 167 + 168 + export const addSeconds = (date: Date, seconds: number): Date => { 169 + const d = cloneDate(date); 170 + 171 + d.setSeconds(getSeconds(d) + seconds); 172 + return d; 173 + }; 174 + 175 + export const addMinutes = (date: Date, minutes: number): Date => { 176 + const d = cloneDate(date); 177 + 178 + d.setMinutes(getMinutes(d) + minutes); 179 + return d; 180 + }; 181 + 182 + export const addHours = (date: Date, hours: number): Date => { 183 + const d = cloneDate(date); 184 + 185 + d.setHours(getHours(d) + hours); 186 + return d; 187 + }; 188 + 189 + export const addDays = (date: Date, days: number): Date => { 190 + const d = cloneDate(date); 191 + 192 + d.setDate(getDayOfMonth(d) + days); 193 + return d; 194 + }; 195 + 196 + export const addMonths = (date: Date, months: number): Date => { 197 + const d = cloneDate(date); 198 + 199 + d.setMonth(getMonth(d) + months); 200 + return d; 201 + }; 202 + 203 + export const addYears = (date: Date, years: number): Date => { 204 + const d = cloneDate(date); 205 + 206 + d.setFullYear(getYear(d) + years); 207 + return d; 208 + }; 209 + 210 + export const previousDay = (date: Date, day: DayOfWeek): Date => { 211 + let delta = day - getDayOfWeek(date); 212 + if (delta >= 0) { 213 + delta -= 7; 214 + } 215 + 216 + return addDays(date, delta); 217 + }; 218 + 219 + export const nextDay = (date: Date, day: DayOfWeek): Date => { 220 + let delta = day - getDayOfWeek(date); 221 + if (delta <= 0) { 222 + delta += 7; 223 + } 224 + 225 + return addDays(date, delta); 226 + };