npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

date-write

v1.0.1

Published

Convert dates to strings with ease.

Downloads

3

Readme

date-write

Convert dates to strings with ease.

Installation

To install date-write just include it to your project like so:

npm install --save date-write

After npm has downloaded the library, you can start importing and using it as normal:

ES6 module*preferred

import { dateToFormat } from "date-write";

CommonJS

const { dateToFormat } = require("date-write");

Getting Started

Now it's time to actually start formatting some dates!

Suppose you want to output the 1st January 2000 as a string in the format Year-Month-Day. Traditionally, this would require you to make calls to the Date.getFullYear(), Date.getMonth() and Date.getDate() methods and then construct the string manually.

With date-write you simply specify your desired output using the supported placeholders and the library will generate the string for you.

For example:

import { dateToFormat } from "date-write";

// Remember: Months in JS are 0 indexed!
const y2k = new Date(2000, 0, 1);
const output = dateToFormat(y2k, "Y-M-D");

console.log(output); // 2000-01-01

The format parameter can be as long or short as you need, and any combination of placeholders can be used.

Formating

At the moment we only support the year, month and day components of the date object but future versions of the library will (hopefully) expose more.

Available Placeholders

| Placeholder | Description | Output | | :--- | :--- | :---: | | Y | 4 digit representation of the year (with leading zeros if required) | 20220774 | | y | The year (no leading zeros) | 2022774 | | M | 2 digit representation of the month (with leading zeros if required) | 1105 | | m | The month (no leading zeros) | 115 | | D | 2 digit representation of the day of the month (with leading zeros if required) | 0731 | | d | The day of the month (no leading zeros) | 731 |

As library standard: uppercased letters will include leading zeros where required, while lowercased letters display the value 'as is'.

Escaping

You may now be asking yourself: "What happens if I want to include any of those letters in my output?"

Luckily, date-write also includes a utility for escaping strings, allowing you to still include characters in the output without them being expanded.

Without escaping you might end up in a situation like this:

import { dateToFormat } from "date-write";

// Remember: Months in JS are 0 indexed!
const myBirthday = new Date(1952, 2, 11);
const output = dateToFormat(myBirthday, "My birthday is: Y-M-D");

console.log(output); // 031952 birth11a1952 is: 1952-03-11

As you can see, the d and y in "birthday" and the M and y in "My" have been expanded to their relevant date components. This is less than ideal.

There are 2 ways to resolve this.

The first is to escape those characters with a backslash. (Because of how JavaScript treats the backslash character, you will have to double escape it!)

import { dateToFormat } from "date-write";

const myBirthday = new Date(1952, 2, 11);
const output = dateToFormat(myBirthday, "\\M\\y birth\\da\\y is: Y-M-D");

console.log(output); // My birthday is: 1952-03-11

Or you can use the bundled escape utility function:

import { dateToFormat, escape } from "date-write";

const myBirthday = new Date(1952, 2, 11);
const label = escape("My birthday is:");
const output = dateToFormat(myBirthday, `${label} Y-M-D`);

console.log(output); // My birthday is: 1952-03-11

Note: Here we're using the backtick template literal syntax, but traditional string concatenation using + or String.concat() will also work just fine.

Examples

That's everything you need to know to get started with date-write, but for the sake of completeness (and to help you see the library in action) here are some examples of what you might choose to use the library for.

Day of the month:

import { dateToFormat, escape } from "date-write";

// ...

const label = escape("day(s) into the month")
const output = dateToFormat(new Date(), `m ${label}`);

console.log(output); // 19 day(s) into the month

Conditional formatting:

import { dateToFormat } from "date-write";

// ...

// Pretend `isUK()` does some locale checking
const format = isUk() ? "d/M/Y" : "m/D/Y";
const output = dateToFormat(new Date(), format);

console.log(output); // Either: 19/01/2023 OR 1/19/2023

Last login date:

import { dateToFormat } from "date-write";

// ...

// Get the last login date somehow
const lastLogin = getLastLogin(user);
const output = dateToFormat(lastLogin, "Last user login: Y-M-D");

console.log(output); // Last user login: 2023-01-19