# date-format

> Formatting Date objects as strings since 2013

Latest version **4.0.14** (published 2022-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install date-format
pnpm add date-format
yarn add date-format
bun add date-format
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.14 |
| Published | 2022-09-26 |
| First published | 2013-08-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4.0 |
| Dependencies | 0 |
| Unpacked size | 19.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 59 |
| Author | Gareth Jones |
| Maintainers | lamweili, csausdev |
| Keywords | date, format, string |

## Links

- npm: https://www.npmjs.com/package/date-format
- Repository: https://github.com/nomiddlename/date-format
- Homepage: https://github.com/nomiddlename/date-format#readme
- Issues: https://github.com/nomiddlename/date-format/issues
- npm.io page: https://npm.io/package/date-format

## Alternatives

- [@js-joda/timezone](https://npm.io/package/@js-joda/timezone.md) — 383.4K weekly downloads
- [chartjs-adapter-moment](https://npm.io/package/chartjs-adapter-moment.md) — 210.8K weekly downloads
- [strftime](https://npm.io/package/strftime.md) — 171.2K weekly downloads
- [vue-flatpickr-component](https://npm.io/package/vue-flatpickr-component.md) — 115.8K weekly downloads
- [timepicker](https://npm.io/package/timepicker.md) — 51.0K weekly downloads

## Recent versions

- 4.0.14 (latest) — 2022-09-26
- 4.0.13 — 2022-07-22
- 4.0.12 — 2022-07-21
- 4.0.11 — 2022-06-02
- 4.0.10 — 2022-05-14
- 4.0.9 — 2022-04-25
- 4.0.8 — 2022-04-25
- 4.0.7 — 2022-04-15
- 4.0.6 — 2022-03-22
- 4.0.5 — 2022-03-14
- 4.0.4 — 2022-03-02
- 4.0.3 — 2022-01-18
- 4.0.2 — 2022-01-14
- 4.0.1 — 2022-01-11
- 3.0.0 — 2019-10-31
- … 8 more at https://npm.io/package/date-format/versions

## README

date-format [![CodeQL](https://github.com/nomiddlename/date-format/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/nomiddlename/date-format/actions/workflows/codeql-analysis.yml) [![Node.js CI](https://github.com/nomiddlename/date-format/actions/workflows/node.js.yml/badge.svg)](https://github.com/nomiddlename/date-format/actions/workflows/node.js.yml)
===========

[![NPM](https://nodei.co/npm/date-format.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/date-format/)

node.js formatting of Date objects as strings. Probably exactly the same as some other library out there.

```sh
npm install date-format
```

usage
=====

Formatting dates as strings
----

```javascript
var format = require('date-format');
format.asString(); // defaults to ISO8601 format and current date
format.asString(new Date()); // defaults to ISO8601 format
format.asString('hh:mm:ss.SSS', new Date()); // just the time
format.asString(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
```

or

```javascript
var format = require('date-format');
format(); // defaults to ISO8601 format and current date
format(new Date()); // defaults to ISO8601 format
format('hh:mm:ss.SSS', new Date()); // just the time
format(format.ISO8601_WITH_TZ_OFFSET_FORMAT, new Date()); // in ISO8601 with timezone
```

**output:**
```javascript
2017-03-14T14:10:20.391
2017-03-14T14:10:20.391
14:10:20.391
2017-03-14T14:10:20.391+11:00
```

Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):
* dd - `date.getDate()`
* MM - `date.getMonth() + 1`
* yy - `date.getFullYear().toString().substring(2, 4)`
* yyyy - `date.getFullYear()`
* hh - `date.getHours()`
* mm - `date.getMinutes()`
* ss - `date.getSeconds()`
* SSS - `date.getMilliseconds()`
* O - timezone offset in ±hh:mm format (note that time will still be local if displaying offset)

Built-in formats:
* `format.ISO8601_FORMAT` - `2017-03-14T14:10:20.391` (local time used)
* `format.ISO8601_WITH_TZ_OFFSET_FORMAT` - `2017-03-14T14:10:20.391+11:00` (local + TZ used)
* `format.DATETIME_FORMAT` - `14 03 2017 14:10:20.391` (local time used)
* `format.ABSOLUTETIME_FORMAT` - `14:10:20.391` (local time used)

Parsing strings as dates
----
The date format library has limited ability to parse strings into dates. It can convert strings created using date format patterns (as above), but if you're looking for anything more sophisticated than that you should probably look for a better library ([momentjs](https://momentjs.com) does pretty much everything).

```javascript
var format = require('date-format');
// pass in the format of the string as first argument
format.parse(format.ISO8601_FORMAT, '2017-03-14T14:10:20.391');
format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+1100');
format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T14:10:20.391+11:00');
format.parse(format.ISO8601_WITH_TZ_OFFSET_FORMAT, '2017-03-14T03:10:20.391Z');
// returns Date
```

---
_Source: https://npm.io/package/date-format · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
