New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

datetime-difference

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

datetime-difference

A lightweight module which finds the difference between two dates in the human-friendly format

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

datetime-difference

npm Dependencies Build Status License File Size File Size (GZip)

A lightweight module which finds the difference between two dates in the human-friendly format. Works almost exactly like moment's duration(...) do, but has no dependencies on any libraries — the code is fast and minimal!

Examples

import datetimeDifference from "datetime-difference";

const date1 = new Date("12/17/2016, 05:23:55 PM");
const date2 = new Date("2/21/2017, 07:12:42 AM");

const result = datetimeDifference(date1, date2); /* result is {
    "years": 0,
    "months": 2,
    "days": 3,
    "hours": 13,
    "minutes": 48,
    "seconds": 47,
    "milliseconds": 0
} */

const date3 = new Date("1/1/2016, 00:00:00 AM");
const date4 = new Date("1/1/2026, 00:00:00 AM");

const result2 = datetimeDifference(date3, date4); /* result2 is {
    "years": 10,
    "months": 0,
    "days": 0,  // 0, because you don't think about leap years as 
    "hours": 0, // well as about daylight time when counting dates!
    "minutes": 0,
    "seconds": 0,
    "milliseconds": 0
} */

// Another example: get the result in readable format by parsing the resulting object
const readme = Object.keys(result)
    .filter(k => !!result[k])
    .map(k => `${ result[k] } ${ k }`)
    .join(", ");
// readme is "2 months, 3 days, 13 hours, 48 minutes, 47 seconds"

// Yet another example of formatting (using the string-format library as an example):
import format from "string-format";
const string = format("{days} days left", result);
// string is "3 days left"

Installation

The datetime-difference is shipped in a form of JavaScript module. Install it from npm:

npm install --save datetime-difference

Licence

MIT © Nikita Savchenko

Keywords

FAQs

Package last updated on 10 May 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc