
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
date-fns-toolkit
Advanced tools
A comprehensive toolkit for working with dates in JavaScript, including timezone support and React integration
A comprehensive toolkit for working with dates in JavaScript, including global timezone support and React integration.
# Using npm
npm install date-fns-toolkit
# Using yarn
yarn add date-fns-toolkit
# Using pnpm
pnpm 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:
// Import directly from date-fns-toolkit instead of date-fns
import {
parse,
differenceInDays,
formatDistance
} from 'date-fns-toolkit';
// date-fns-tz functions are also available
import {
formatInTimeZone,
toZonedTime
} from 'date-fns-toolkit';
// Use just like you would with date-fns and date-fns-tz
const 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.
import { setDefaultTimezone, formatDateTime, addDays } from 'date-fns-toolkit';
// Set the default timezone for your entire application
setDefaultTimezone('America/New_York');
// Now you can use date utilities without specifying timezone
const now = new Date();
console.log(`Current date and time: ${formatDateTime(now)}`);
console.log(`Tomorrow: ${formatDateTime(addDays(now, 1))}`);
// You can still override the timezone for specific calls
console.log(`Tokyo time: ${formatDateTime(now, 'Asia/Tokyo')}`);
import React from 'react';
// Import only the functions you need
import { formatDateTime, addDays, isBefore } from 'date-fns-toolkit';
function EventItem({ event }) {
// Uses the global default timezone
const displayTime = formatDateTime(event.startTime);
// Get tomorrow
const tomorrow = addDays(new Date(), 1);
// Check if event is before tomorrow
const isToday = isBefore(event.startTime, tomorrow);
return (
<div>
<h3>{event.title}</h3>
<p>Time: {displayTime}</p>
{isToday && <span>Today!</span>}
</div>
);
}
import React from 'react';
import { useDateTimezone } from 'date-fns-toolkit';
function ClockDisplay() {
// Hook will use the global default timezone if none provided
const dateUtils = useDateTimezone();
const [time, setTime] = React.useState(new Date());
React.useEffect(() => {
const timer = setInterval(() => setTime(new Date()), 1000);
return () => clearInterval(timer);
}, []);
return (
<div>
<h2>Current Time</h2>
<p>{dateUtils.formatDateTime(time)}</p>
<p>Timezone: {dateUtils.getTimezone()}</p>
</div>
);
}
import React from 'react';
import { TimezoneProvider, useTimezoneContext } from 'date-fns-toolkit';
function App() {
return (
// syncWithGlobal will update the global default when context changes
<TimezoneProvider syncWithGlobal={true}>
<AppContent />
</TimezoneProvider>
);
}
function AppContent() {
const { timezone, setTimezone } = useTimezoneContext();
const handleTimezoneChange = (e) => {
// This will update both context AND the global default
setTimezone(e.target.value);
};
return (
<div>
<h1>Timezone Settings</h1>
<select value={timezone} onChange={handleTimezoneChange}>
<option value="America/New_York">New York</option>
<option value="Europe/London">London</option>
<option value="Asia/Tokyo">Tokyo</option>
</select>
{/* All components will use the selected timezone */}
<DateDisplay />
<EventCalendar />
</div>
);
}
All functions from date-fns
and date-fns-tz
are available:
import {
format,
parse,
addDays,
addMonths,
differenceInDays,
isAfter,
isBefore,
formatInTimeZone,
toZonedTime,
fromZonedTime
// ...and many more
} from 'date-fns-toolkit';
For documentation on these functions, refer to:
setDefaultTimezone(timezone: string): void
Sets the global default timezone for all date functions.
import { setDefaultTimezone } from 'date-fns-toolkit';
setDefaultTimezone('America/New_York');
getDefaultTimezone(): string
Gets the current global default timezone.
import { getDefaultTimezone } from 'date-fns-toolkit';
console.log(`Current timezone: ${getDefaultTimezone()}`);
useDateTimezone(timezone?: string)
Hook that returns timezone-aware date utility functions.
timezone
is provided, it will use that timezoneconst dateUtils = useDateTimezone('Europe/Paris');
// OR
const dateUtils = useDateTimezone(); // Uses global default
useTimezoneContext()
Hook to access the timezone context when using TimezoneProvider
.
const { 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
:
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
// Fix for date-fns-toolkit module parse error
config.module.rules.push({
test: /node_modules\/date-fns-toolkit\/dist\/index\.esm\.js$/,
type: 'javascript/auto',
resolve: {
fullySpecified: false
}
});
return config;
}
};
module.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.
FAQs
A comprehensive toolkit for working with dates in JavaScript, including timezone support and React integration
The npm package date-fns-toolkit receives a total of 3 weekly downloads. As such, date-fns-toolkit popularity was classified as not popular.
We found that date-fns-toolkit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.