Reputation: 15613
How can I convert Gregorian date
to Persian date
and vice-versa in Python
?
All I found was some widgets and stuffs which will create Persian Calendar. I don't want a Persian calendar. I just want to convert dates to each other. So how can I do that?
Upvotes: 76
Views: 37566
Reputation: 71
First you have to install the jdatetime
with pip
pip install jdatetime
the library Jdateimte
is so weak and non-friendly without any valid documentation, almost everyone get confused over it (thanks to it's Iranian development).
but here's the drill:
timestamp = 1647334174.7492166
jdatetime.datetime.fromtimestamp(timestamp)
#returns 'jdatetime.datetime(1400, 12, 24, 1, 48, 17, 532053)'
From your 'datetime' object:
yourDate = datetime.datetime.now()
jdatetime.datetime.fromtimestamp(inputDate.timestamp())
#returns same result as before.
Upvotes: 7
Reputation: 11
pip install jdatetime
import jdatetime
gregorian_date = jdatetime.date(1400,5,24).togregorian()
jalili_date = jdatetime.date.fromgregorian(day=1,month=12,year=2021)
Upvotes: 0
Reputation: 6554
Try this:
import jdatetime
jalili_date = jdatetime.datetime.fromgregorian(day=4, month=9, year=2022, hour=9, minute=0, second=0)
print(jalili_date)
Upvotes: 0
Reputation: 121
I didnt see khayyam package in the answers so here is the link:
https://pypi.org/project/Khayyam/
it is faster than jdatetime and better maintained
Upvotes: 3
Reputation: 18838
Also, you can use jdatetime
library like the following:
import jdatetime
gregorian_date = jdatetime.date(1396,2,30).togregorian()
jalili_date = jdatetime.date.fromgregorian(day=19,month=5,year=2017)
See other functions in details in this document.
Upvotes: 64
Reputation: 1723
You can use PersianTools library:
Example:
>>> from persiantools.jdatetime import JalaliDate
>>> import datetime
>>> JalaliDate.today()
JalaliDate(1395, 4, 18, Jomeh)
>>> JalaliDate(datetime.date(1990, 9, 23)) # Gregorian to Jalali
JalaliDate(1369, 7, 1, Yekshanbeh)
>>> JalaliDate.to_jalali(2013, 9, 16) # Gregorian to Jalali
JalaliDate(1392, 6, 25, Doshanbeh)
>>> JalaliDate(1392, 6, 25).to_gregorian() # Jalali to Gregorian
datetime.date(2013, 9, 16)
>>> JalaliDate.fromtimestamp(578707200) # Timestamp to Jalali
JalaliDate(1367, 2, 14, Chaharshanbeh)
Upvotes: 19
Reputation: 91
import jdatetime
fa_date = jdatetime.date.today()
fa_date.j_months_fa[0]
jdatetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S")#'Tue, 05 Far 1399 15:49:44'
jdatetime.datetime.now().strftime("%y %m %d") #'99 01 05'
jdatetime.datetime.now().strftime("%Y %m %d") # '1399 01 05'
jdatetime.datetime.now().strftime('%A %B')
import jdatetime
jalili_date = jdatetime.date(1399,1,5).togregorian()
gregorian_date = jdatetime.date.fromgregorian(day=24 ,month=3,year=2020)
Upvotes: 8