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) | 2022
0774
|
| y
| The year (no leading zeros) | 2022
774
|
| M
| 2 digit representation of the month (with leading zeros if required) | 11
05
|
| m
| The month (no leading zeros) | 11
5
|
| D
| 2 digit representation of the day of the month (with leading zeros if required) | 07
31
|
| d
| The day of the month (no leading zeros) | 7
31
|
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