DEV Community

Joodi
Joodi

Posted on

3 1 1 1 1

Convert Dates to Persian Time in React

PersianTime is a simple utility to convert any date or time to Iran's local time with Persian date formatting. Whether you're building an application for Iranian users or need to handle dates in the Persian calendar, this library has got you covered!

Image description

Features

  • Convert any given date or time into Iran's local time (UTC +3:30).
  • Persian date formatting for easy reading in Iranian format.
  • Easy-to-use function for quick integration into any JavaScript/TypeScript project.

Installation

You can easily install PersianTime using npm:

npm install persiantime
Enter fullscreen mode Exit fullscreen mode

Or, if you're using Yarn:

yarn add persiantime
Enter fullscreen mode Exit fullscreen mode

Usage

Once installed, you can import and use the PersianTime function to format any date or timestamp into Persian time.

Example:

import { PersianTime } from 'persiantime';

const persianFormattedTime = PersianTime(new Date('2025-01-20T11:05:20.451Z'));

console.log(persianFormattedTime);

Enter fullscreen mode Exit fullscreen mode

In the example above, you can see that the date is converted to Persian format and Tehran local time is applied.

Handling Dates from APIs:

If you receive a date from an API in ISO format, you can use the following approach to convert it:

import { PersianTime } from 'persiantime';

const createdAt = PersianTime(new Date(document.created_at));
const updatedAt = PersianTime(new Date(document.updated_at));

console.log(createdAt);
console.log(updatedAt);

Enter fullscreen mode Exit fullscreen mode

Why PersianTime?

Accurate Time Conversion: Automatically adjusts to Iran's time zone (UTC+3:30).
Easy Integration: Works with both JavaScript and TypeScript projects.
Simple and Lightweight: Focused solely on converting date and time with minimal dependencies.
Contributing
PersianTime is an open-source project and contributions are always welcome! Feel free to fork the repo, make improvements, and create pull requests. If you encounter any issues or have suggestions for new features, please open an issue on the GitHub repository.

Support

If you find this library helpful, please consider giving it a star on GitHub and leave a review on npm. Your support will encourage me to continue improving it and adding more features.

Links

πŸ”— PersianTime on npm
πŸ“¦ PersianTime GitHub Repository

Thanks for using PersianTime! 🌟

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay