Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

diff-ymd-package

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

diff-ymd-package

Utility class for calculating the difference between two dates in (aY bM cD)(aYears bMonths cDays) or customized formats like aY-bM-cD or aYears-bMonths-cDays etc.

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

diff-ymd-package

diff-ymd-package a javascript package provides APIs to difference dates in formatted ways(like (aYears bMonths cDays) or (aY bM cD) etc., eg. age = 20Y 2M 23D or datesDifference = 2Years 11Months 20Days)

NPM Version CI License

Installation

Install from npm registry

npm install diff-ymd-package

Install from Github Packages registry

npm install @farhan7reza7/diff-ymd-package

Usage

//const DatesYMD = require('@farhan7reza7/diff-ymd-package'); or

const DatesYMD = require('diff-ymd-package'); // can use any

const date1 = '2022-01-01';
const date2 = '2023-12-31';

const calculator = new DatesYMD(date1, date2);

const result = calculator.formattedYMD();
const resultArray = calculator.diffArray();

console.log(result); // Output: "1Y 11M 30D"
// formatted output in aY bM cD format

console.log(resultArray); // Output: [1, 11, 30, '1Y 11M 30D']
/* you can access each of Y, M, D separately from output array and can format as per your choice like aY-bM-cD or aYears-bMonths-cDays etc.*/

/*example: 
let Y, M, D;

Y = resultArray[0];
M = resultArray[1];
D = resultArray[2];

const customFormat = Y + 'years ' + M + 'months ' + D + 'days';
console.log(customFormat); // output: 1years 11months 30days
*/

API Documentation

DatesYMD

Represents a utility class for calculating the difference between two dates.

Create an instance of DatesYMD:
const Formatter = new DatesYMD(firstDate, secondDate);
  • firstDate: The first date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or 'yyyy.mm.dd'.
  • secondDate: The second date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or 'yyyy.mm.dd'.
Methods:

diffArray Calculates the difference between two dates and returns an array containing Y(years), M(months), D(days), and a formatted 'aY bM cD' difference string.

const result = calculator.diffArray();
  • Returns: An array containing the calculated years, months, days, and the formatted difference.

formattedYMD() Returns the formatted difference between two dates in aY bM cD(aYears bMonths cDays) format.

const result = calculator.formattedYMD();
  • Returns: A string in the format 'aY bM cD'.

For more information, see diff-ymd-package documentation

Contributing

If you find any issues or have suggestions for improvement, please open an issue or create a pull request on the GitHub repository.

See CONTRIBUTING.md for more informations.

Best Practices:

  • Meaningful variable and function names for clarity.
  • Clear and concise comments to enhance understanding.
  • Proper indentation and formatting for visual organization.

License

This project is licensed under the MIT License - see the LICENSE file for details.

See License

History

For more details about what has changed in each version of this project, see the CHANGELOG.
See CHANGELOG.md.

Keywords

FAQs

Package last updated on 17 Jan 2024

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