Gathering detailed insights and metrics for date-fns-toolkit
Gathering detailed insights and metrics for date-fns-toolkit
Gathering detailed insights and metrics for date-fns-toolkit
Gathering detailed insights and metrics for date-fns-toolkit
A comprehensive toolkit for working with dates in JavaScript, including global timezone support and React integration.
npm install date-fns-toolkit
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (96.43%)
JavaScript (3.57%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
34 Commits
1 Forks
1 Watchers
1 Branches
3 Contributors
Updated on May 17, 2025
Latest Version
1.3.0
Package Id
date-fns-toolkit@1.3.0
Unpacked Size
194.48 kB
Size
29.20 kB
File Count
25
NPM Version
10.9.2
Node Version
20.19.1
Published on
May 17, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
31
A comprehensive toolkit for working with dates in JavaScript, including global timezone support and React integration.
1# Using npm 2npm install date-fns-toolkit 3 4# Using yarn 5yarn add date-fns-toolkit 6 7# Using pnpm 8pnpm add date-fns-toolkit
No need to install date-fns or date-fns-tz separately - they're included in the package!
Most date-fns and all date-fns-tz functions are re-exported from this package:
1// Import directly from date-fns-toolkit instead of date-fns 2import { 3 parse, 4 differenceInDays, 5 formatDistance 6} from 'date-fns-toolkit'; 7 8// date-fns-tz functions are also available 9import { 10 formatInTimeZone, 11 toZonedTime 12} from 'date-fns-toolkit'; 13 14// Use just like you would with date-fns and date-fns-tz 15const formattedDate = formatDistance(new Date(), new Date(2021, 0, 1));
Note: Some core date-fns functions (like
format
,addDays
, etc.) are overridden with timezone-aware versions. If you need the original behavior, import directly from date-fns.
1import { setDefaultTimezone, formatDateTime, addDays } from 'date-fns-toolkit'; 2 3// Set the default timezone for your entire application 4setDefaultTimezone('America/New_York'); 5 6// Now you can use date utilities without specifying timezone 7const now = new Date(); 8console.log(`Current date and time: ${formatDateTime(now)}`); 9console.log(`Tomorrow: ${formatDateTime(addDays(now, 1))}`); 10 11// You can still override the timezone for specific calls 12console.log(`Tokyo time: ${formatDateTime(now, 'Asia/Tokyo')}`);
1import React from 'react'; 2// Import only the functions you need 3import { formatDateTime, addDays, isBefore } from 'date-fns-toolkit'; 4 5function EventItem({ event }) { 6 // Uses the global default timezone 7 const displayTime = formatDateTime(event.startTime); 8 9 // Get tomorrow 10 const tomorrow = addDays(new Date(), 1); 11 12 // Check if event is before tomorrow 13 const isToday = isBefore(event.startTime, tomorrow); 14 15 return ( 16 <div> 17 <h3>{event.title}</h3> 18 <p>Time: {displayTime}</p> 19 {isToday && <span>Today!</span>} 20 </div> 21 ); 22}
1import React from 'react'; 2import { useDateTimezone } from 'date-fns-toolkit'; 3 4function ClockDisplay() { 5 // Hook will use the global default timezone if none provided 6 const dateUtils = useDateTimezone(); 7 const [time, setTime] = React.useState(new Date()); 8 9 React.useEffect(() => { 10 const timer = setInterval(() => setTime(new Date()), 1000); 11 return () => clearInterval(timer); 12 }, []); 13 14 return ( 15 <div> 16 <h2>Current Time</h2> 17 <p>{dateUtils.formatDateTime(time)}</p> 18 <p>Timezone: {dateUtils.getTimezone()}</p> 19 </div> 20 ); 21}
1import React from 'react'; 2import { TimezoneProvider, useTimezoneContext } from 'date-fns-toolkit'; 3 4function App() { 5 return ( 6 // syncWithGlobal will update the global default when context changes 7 <TimezoneProvider syncWithGlobal={true}> 8 <AppContent /> 9 </TimezoneProvider> 10 ); 11} 12 13function AppContent() { 14 const { timezone, setTimezone } = useTimezoneContext(); 15 16 const handleTimezoneChange = (e) => { 17 // This will update both context AND the global default 18 setTimezone(e.target.value); 19 }; 20 21 return ( 22 <div> 23 <h1>Timezone Settings</h1> 24 <select value={timezone} onChange={handleTimezoneChange}> 25 <option value="America/New_York">New York</option> 26 <option value="Europe/London">London</option> 27 <option value="Asia/Tokyo">Tokyo</option> 28 </select> 29 30 {/* All components will use the selected timezone */} 31 <DateDisplay /> 32 <EventCalendar /> 33 </div> 34 ); 35}
All functions from date-fns
and date-fns-tz
are available:
1import { 2 format, 3 parse, 4 addDays, 5 addMonths, 6 differenceInDays, 7 isAfter, 8 isBefore, 9 formatInTimeZone, 10 toZonedTime, 11 fromZonedTime 12 // ...and many more 13} from 'date-fns-toolkit';
For documentation on these functions, refer to:
setDefaultTimezone(timezone: string): void
Sets the global default timezone for all date functions.
1import { setDefaultTimezone } from 'date-fns-toolkit'; 2 3setDefaultTimezone('America/New_York');
getDefaultTimezone(): string
Gets the current global default timezone.
1import { getDefaultTimezone } from 'date-fns-toolkit'; 2 3console.log(`Current timezone: ${getDefaultTimezone()}`);
useDateTimezone(timezone?: string)
Hook that returns timezone-aware date utility functions.
timezone
is provided, it will use that timezone1const dateUtils = useDateTimezone('Europe/Paris'); 2// OR 3const dateUtils = useDateTimezone(); // Uses global default
useTimezoneContext()
Hook to access the timezone context when using TimezoneProvider
.
1const { timezone, setTimezone } = useTimezoneContext();
TimezoneProvider
Context provider for application-wide timezone settings.
Props:
children
: React childrendefaultTimezone
: (optional) Initial timezone to use. Falls back to global default if not providedsyncWithGlobal
: (optional, default: false) When true, changes to context timezone will also update the global defaultAll toolkit utility functions accept an optional timezone parameter. If not provided, they use the global default timezone.
format(date, formatString, timezone?, options?)
formatDateShort(date, timezone?)
formatDateLong(date, timezone?)
formatDateTime(date, timezone?)
formatTime(date, timezone?)
toDate(date, timezone?)
toZonedTime(date, timezone?)
fromZonedTime(zonedDate, timezone?)
parseInTimeZone(dateString, formatString, timezone?)
addDays(date, amount, timezone?)
subDays(date, amount, timezone?)
addMonths(date, amount, timezone?)
subMonths(date, amount, timezone?)
addYears(date, amount, timezone?)
subYears(date, amount, timezone?)
startOfDay(date, timezone?)
endOfDay(date, timezone?)
isBefore(date1, date2, timezone?)
isAfter(date1, date2, timezone?)
isSameDay(date1, date2, timezone?)
If you're using date-fns-toolkit with Next.js, you might encounter module loading issues. We've provided a detailed guide to help you resolve them:
We also have a complete example of using date-fns-toolkit in a Next.js application:
Quick fix: Add this to your next.config.js
:
1/** @type {import('next').NextConfig} */ 2const nextConfig = { 3 webpack: (config) => { 4 // Fix for date-fns-toolkit module parse error 5 config.module.rules.push({ 6 test: /node_modules\/date-fns-toolkit\/dist\/index\.esm\.js$/, 7 type: 'javascript/auto', 8 resolve: { 9 fullySpecified: false 10 } 11 }); 12 13 return config; 14 } 15}; 16 17module.exports = nextConfig;
date-fns-toolkit is designed to work in all JavaScript environments:
The package provides multiple module formats to ensure compatibility:
dist/index.js
(for Node.js and most bundlers)dist/index.mjs
(for modern bundlers and environments)dist/index.esm.js
(for backward compatibility)In most cases, your bundler or environment will automatically choose the right format.
Current version: 1.3.0
MIT
If you find my packages helpful or are interested in the platform I'm building, I'd really appreciate a coffee! Your support helps me dedicate more time to open source projects and platform development.
No vulnerabilities found.
No security vulnerabilities found.