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 🙏

© 2024 – Pkg Stats / Ryan Hefner

date-string-fns

v1.0.1

Published

A set of functions for handling ISO-8601 date strings

Downloads

3

Readme

date-string-fns

A library for manipulating ISO8601 date strings.

An ISO8601 date is a string in the format yyyy-mm-dd - 10 characters, consisting of a 4-digit year, 2-digit month and 2-digit day, separated by dashes. Examples of ISO8601 dates:

  • '2013-06-30' - 30th June 2013
  • '1900-01-01' - 1st January 1900
  • '1776-07-04' - July 4th 1776
  • '1066-10-14' - 14th October 1066
  • '0815-06-28' - June 28th 815

Installation

npm install date-string-fns
yarn add date-string-fns

Usage

Conversion to ISO8601

From a Javascript Date object using the local time value:

import { fromLocalDate } from 'date-string-fns';

const iso = fromLocalDate(new Date()); // '2013-05-31'

From a Javascript Date object using UTC time value:

import { fromUTCDate } from 'date-string-fns';

const iso = fromUTCDate(new Date()); // '2013-05-31'

From raw day, month and year values:

import { fromDMY } from 'date-string-fns';

const iso = fromDMY({ day: 4, month: 7, year: 1776 }); // '1776-07-04'

Convenience functions

import { todaysDate, tomorrowsDate, yesterdaysDate } from 'date-string-fns';

const today = todaysDate(); // '2013-02-14'
const tomorrow = tomorrowsDate(); // '2013-02-15'
const yesterday = yesterdaysDate(); // '2013-02-13'

Conversion from ISO8601

To a local Javascript Date object

import { toLocalDate } from 'date-string-fns';

const date = toLocalDate('2013-02-14'); // 14th Feb 2013 at midnight (local time)

To a UTC Javascript Date object

import { toUTCDate } from 'date-string-fns';

const date = toUTCDate('2013-02-14', { h: 16, m: 31, s: 45, ms: 678 }); // 14th Feb 2013 at 16:31:45.678 (utc)

To day, month and year values

import { toDMY } from 'date-string-fns';

const { day, month, year } = toDMY('2013-04-26'); // { day: 26, month: 4, year: 2013 }

Adding / subtracting dates

import { addDate } from 'date-string-fns';

const oneMonthLater = addDate('2023-02-14', { months: 1 });
console.log(oneMonthLater); // '2023-03-14'

const eightySixDaysEarlier = addDate('2023-02-14', { days: -86 });
console.log(eightySixDaysEarlier); // '2022-11-20'

const oneDayTwoMonthsAndThreeYearsLater = addDate('2023-02-14', {
  days: 1,
  months: 2,
  years: 3,
});
console.log(oneDayTwoMonthsAndThreeYearsLater); // '2026-03-02'

const lastDayOf2023 = addDate('2023-01-01', { years: 1, days: -1 });
console.log(lastDayOf2023); // '2023-12-31'

// note that when incrementing by months, the day is altered to suit the resulting month:
// - if the date passed in is the last day of the month, the result will also have the last day of the month
// - otherwise the date is the closest valid date for the resulting month
const jan31stPlusOneMonth = addDate('2023-01-31', { months: 1 });
console.log(jan31stPlusOneMonth); // '2023-02-28'