
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
The strtime package provides native JavaScript implementations of the
strftime and strptime functions.
It supports most of the combined features of
Python,
Ruby,
and GNU C
strftime and strptime functions.
It's possible to write and parse calendar dates (e.g. %Y-%m-%d
),
week dates (e.g. %G-W%V-%u
), and ordinal dates (e.g. %Y-%j
).
You can add the strtime package to your project using a JavaScript package manager such as npm.
npm install --save strtime
You can read complete documentation regarding the directives which strtime
supports (e.g. %Y
, %b
) and how they behave in directives.md.
The strftime function accepts Date objects, unix timestamps (as milliseconds since UTC epoch), moment datetime objects, luxon datetime objects, and dayjs datetime objects. The strptime function always outputs a Date object.
const strftime = require("strtime").strftime;
const strptime = require("strtime").strptime;
// Prints "2000-01-01T00:00:00.000Z"
console.log(strftime(new Date("2000-01-01"), "%Y-%m-%dT%H:%M:%S.%LZ"));
// Prints "2000-01-01T00:00:00.000Z"
const date = strptime("2000-01-01T00:00:00.000Z", "%Y-%m-%dT%H:%M:%S.%LZ");
console.log(date.toISOString());
The strftime function defaults to writing a UTC timestamp.
You can specify which timezone should be used by passing it as an argument. Timezones are accepted as numeric offsets or as abbreviations such as UTC
or EDT
or EEST
. Offsets between and including -16 and +16 are interpreted as hour offsets. Other offset values are interpreted as minute offsets. You can also use the string local
to use the local timezone.
// Prints "2000-01-01 12:00:00 GMT+0000"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z"));
// Prints "2000-01-01 14:00:00 GMT+0200"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z", +2));
// Prints "2000-01-01 08:00:00 GMT-0400"
console.log(strftime(new Date("2000-01-01T12:00:00Z"), "%Y-%m-%d %H:%M:%S GMT%z", "EDT"));
The strptime function assumes that a timestamp represents a UTC date if no timezone is specified in that timestamp. You can specify what timezone should be assumed for timestamps which do not contain an explicit timezone by passing it as an argument. The strptime function accepts timezones arguments in the same way that strftime does.
// Prints e.g. "2000-01-01T12:00:00.000Z"
const date = strptime("2000-01-01 12:00:00", "%Y-%m-%d %H:%M:%S");
console.log(date.toISOString());
// Prints "2000-01-01T10:00:00.000Z" due to the +2 hours offset
const date = strptime("2000-01-01 12:00:00", "%Y-%m-%d %H:%M:%S", +2);
console.log(date.toISOString());
The strftime and strptime functions default to English weekday names, month names, and ordinals. However, it is possible to specify different text by passing an options object.
The options object attributes which are recognized for this purpose are:
const monthNames = [
"enero", "febrero", "marzo", "abril", "mayo", "junio",
"julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"
];
// Prints "1 enero 2000"
console.log(strftime(new Date("2000-01-01"), "%-d %B %Y", +0, {
longMonthNames: monthNames
}));
FAQs
Comprehensive strftime and strptime implementation.
The npm package strtime receives a total of 316 weekly downloads. As such, strtime popularity was classified as not popular.
We found that strtime demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.