Created
December 15, 2023 23:00
-
-
Save AliAlmasi/9b93ade35d6351404a82d2b42981e098 to your computer and use it in GitHub Desktop.
A better way to use Persian Calendar in JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PersianDate.js | |
A better way to use Persian Calendar in JS. | |
Thanks to Amir Forsati (@pwwiur) https://stackoverflow.com/a/61158379 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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