Skip to content

Instantly share code, notes, and snippets.

@senior-x-79
Created January 20, 2022 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senior-x-79/1d8163b30ff91c58eff234618e16b71f to your computer and use it in GitHub Desktop.
Save senior-x-79/1d8163b30ff91c58eff234618e16b71f to your computer and use it in GitHub Desktop.
تبدیل تاریخ از شمسی به میلادی یا میلادی به شمسی با 2 روش در جاوا اسکریپت
// METHOD 1
const today = Date.now();
const todayFa = {
"day" : getDateFormat(today , {"day" : "2-digit"}),
"month" : getDateFormat(today , {"month" : "numeric"}),
"monthTitle" : getDateFormat(today , {"month" : "long"}),
"year" : getDateFormat(today , {"year" : "numeric"}),
"dayWeek" : getDateFormat(today , {"weekday" : "long"}),
}
function getDateFormat(uDate,option){
let date = new Intl.DateTimeFormat('fa-IR', option).format(uDate);
return date;
}
console.log(todayFa);
/*
{
day: "۰۱",
dayWeek: "جمعه",
month: "۱۱",
monthTitle: "بهمن",
year: "۱۴۰۰"
}
*/
// METHOD 2
// first add farvardin.js library from https://github.com/senior-x-79/farvardin.js/tree/master/dist
// ### <script src="static/js/lib/farvardin.min.js"></script>
// from persian to normal date
console.log(farvardin.solarToGregorian(1399 , 1 , 25)); // [2020 , 4 , 13]
console.log(farvardin.solarToGregorian(1399 , 1 , 25 , "array")); // همان خروجی بالا
// from normal date to persian
console.log(farvardin.gregorianToSolar(2020 , 4 , 13 , "string")); // 1399-01-25
console.log(farvardin.gregorianToSolar(2020 , 4 , 13 , "object")); // { year: 1399, month: 1, day: 25 }
console.log(farvardin.gregorianToSolar(2020 , 4 , 13 , "json")); // {"year":1399,"month":1,"day":25}
@senior-x-79
Copy link
Author

مقاله تبدیل تاریخ میلادی به شمسی یا بالعکس در جاوا اسکریپت را از اینجا مطالعه کنید .

به به زیتون رود بار 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment