Last active
February 7, 2018 09:27
-
-
Save rzkhosroshahi/7301fcd0b4b017c409856549b34db5c6 to your computer and use it in GitHub Desktop.
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
const StringFarsiMonthes = (str) => { | |
switch (str) { | |
case "۱": | |
return 'فروردین'; | |
case "۲": | |
return 'اردیبهشت'; | |
case "۳": | |
return 'خرداد'; | |
case "۴": | |
return 'تیر'; | |
case "۵": | |
return 'مرداد'; | |
case "۶": | |
return 'شهریور'; | |
case "۷": | |
return 'مهر'; | |
case "۸": | |
return 'آبان'; | |
case "۹": | |
return 'آذر'; | |
case "۱۰": | |
return 'دی'; | |
case "۱۱": | |
return 'بهمن'; | |
case "۱۲": | |
return 'اسفند'; | |
default: | |
return null; | |
} | |
} | |
function faYear (str) { | |
return str.match(/[۰-۹]{4}/gi).join(''); | |
} | |
function faMonth (str) { | |
return StringFarsiMonthes(str.match(/\/[۰-۹]*\//gi).join('').match(/[۰-۹]*/gi).join('')); | |
} | |
function faDay (str) { | |
return str.match(/\/.[۰-۹]$/gi).join('').match(/[۰-۹]*/gi).join(''); | |
} | |
function dateFaSeperator(date) { | |
const farsiDate = date.toLocaleDateString('fa'); | |
const year = faYear(farsiDate); | |
const month = faMonth(farsiDate); | |
const day = faDay(farsiDate); | |
return { | |
year, | |
month, | |
day, | |
}; | |
} | |
console.log(dateFaSeperator(new Date())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment