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

@mu-utils/persian-date

v1.0.7

Published

A Persian date to convert from gregorian and behave like a date in Javascript project.

Downloads

252

Readme

PersianDate

A JavaScript library for working with Persian (Jalali) dates, extending the native JavaScript Date object.

Table of Contents

Installation

npm install @mu-utils/persian-date

Usage

import PersianDate from "@mu-utils/persian-date";

const persianDate = new PersianDate();
console.log(persianDate.format("YYYY/MM/DD"));
console.log(persianDate.format("YYYY/MM/DD HH:mm:ss"));

Features

  • Supports both Persian (Jalali) and Gregorian calendars
  • Extends the native JavaScript Date object
  • Flexible date formatting
  • Date manipulation (add, subtract)
  • Date comparison and difference calculation
  • Leap year detection

API

Constructor

The PersianDate constructor supports multiple overloads:

new PersianDate();
new PersianDate(options);
new PersianDate(value, options);
new PersianDate(year, month, options);
new PersianDate(year, month, date, options);
new PersianDate(year, month, date, hours, options);
new PersianDate(year, month, date, hours, minutes, options);
new PersianDate(year, month, date, hours, minutes, seconds, options);
new PersianDate(year, month, date, hours, minutes, seconds, ms, options);

Methods

setTimeZone(timeZone: TimeZone): void

Sets the time zone for the PersianDate instance.

setCalendar(calendar: Calendar): void

Sets the calendar used by the PersianDate instance.

format(template: DateFormatTemplate): string

Formats the current PersianDate instance using the provided date format template.

diff(value: DateValue, unit?: DateUint): number

Calculates the difference between the current PersianDate instance and the provided date value.

add(unit: DateUint, value: number): PersianDate

Adds the specified time unit and value to the current PersianDate instance.

subtract(unit: DateUint, value: number): PersianDate

Subtracts the specified time unit and value from the current PersianDate instance.

getFullYear(): number

Gets the full year of the current PersianDate instance.

getDate(): number

Gets the day of the month for the current PersianDate instance.

getMonth(): number

Gets the month for the current PersianDate instance.

isLeapYear(): boolean

Determines if the current year is a leap year based on the selected calendar.

Algorithm

The conversion from Gregorian to Persian (Jalali) dates is based on an algorithm that involves the following steps:

  1. Ephemeris Base Calculation: The base year is adjusted depending on whether the year is negative or positive.

    const epbase = year - (year >= 0 ? 474 : 473);
  2. Ephemeris Year Calculation: This determines the ephemeris year based on the adjusted base.

    const epyear = 474 + mod(epbase, 2820);
  3. Day of Year Calculation: The day of the year is computed by considering whether the month is within the first seven months or the latter five.

    const dayOfYear =
      day + (month <= 7 ? (month - 1) * 31 : (month - 1) * 30 + 6);
  4. Total Days Calculation: The total number of days is calculated based on the ephemeris year, accounting for leap years.

    const yearDays = (epyear - 1) * 365;
    const leapYears = Math.floor(epbase / 2820) * 1029983;
    const leapYearDays = Math.floor((epyear * 682 - 110) / 2816);
  5. Leap Year Adjustment: The algorithm checks if the current year is a leap year and makes necessary adjustments based on the month.

    const isLeap = isPersianLeapYear(year);
    const extraDay = isLeap && month > 6 ? 1 : 0;

This algorithm ensures accurate conversion between the two calendar systems, handling the complexities of leap years and month lengths.

Examples

1. Create a Persian Date Instance

Create an instance of PersianDate using the current date:

import PersianDate from "@mu-utils/persian-date";

const currentDate = new PersianDate();
console.log(currentDate.format("YYYY/MM/DD")); // e.g., "1403/07/01"

2. Create a Persian Date with Specific Date

Create a Persian date using a specific year, month, and day:

const specificDate = new PersianDate(1402, 12, 25);
console.log(specificDate.format("YYYY/MM/DD")); // e.g., "1402/12/25"

3. Format a Date

Format a Persian date instance using a custom template:

const formattedDate = currentDate.format("YYYY/MM/DD HH:mm:ss");
console.log(formattedDate); // e.g., "1403/07/01 12:34:56"

4. Add Days to a Date

Add days to the current date:

const futureDate = currentDate.add("days", 10);
console.log(futureDate.format("YYYY/MM/DD")); // e.g., "1403/07/11"

5. Subtract Months from a Date

Subtract months from a specific date:

const pastDate = specificDate.subtract("months", 3);
console.log(pastDate.format("YYYY/MM/DD")); // e.g., "1402/09/25"

6. Calculate the Difference Between Dates

Calculate the difference in days between two dates:

const anotherDate = new PersianDate(1403, 07, 01);
const daysDifference = anotherDate.diff(specificDate, "days");
console.log(`Difference in days: ${daysDifference}`); // e.g., "Difference in days: 90"

7. Check for Leap Year

Check if the current year is a leap year:

const isLeap = currentDate.isLeapYear();
console.log(`Is the current year a leap year? ${isLeap}`); // e.g., "Is the current year a leap year? true"

License

MIT License