Skip to content

Instantly share code, notes, and snippets.

@AliAlmasi
Created December 15, 2023 23:00
Show Gist options
  • Save AliAlmasi/9b93ade35d6351404a82d2b42981e098 to your computer and use it in GitHub Desktop.
Save AliAlmasi/9b93ade35d6351404a82d2b42981e098 to your computer and use it in GitHub Desktop.
A better way to use Persian Calendar in JS.
PersianDate.js
A better way to use Persian Calendar in JS.
Thanks to Amir Forsati (@pwwiur) https://stackoverflow.com/a/61158379
class PersianDate extends Date {
constructor(...args) {
super(...args);
}
toLocaleDateString = () => super.toLocaleDateString("fa-IR-u-nu-latn");
getParts = () => this.toLocaleDateString().split("/");
getDay = () => (super.getDay() === 6 ? 0 : super.getDay() + 1);
getDate = () => this.getParts()[2];
getMonth = () => this.getParts()[1] - 1;
getYear = () => this.getParts()[0];
getMonthName = () => this.toLocaleDateString("fa-IR", { month: "long" });
getDayName = () => this.toLocaleDateString("fa-IR", { weekday: "long" });
}
// USAGE:
const currentYear = new PersianDate().getYear();
console.log(currentYear)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment