Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Gregorian (Christian) date to Persian date and vice-versa in Python

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?

like image 532
Alex Jolig Avatar asked Apr 15 '15 15:04

Alex Jolig


3 Answers

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.

like image 140
OmG Avatar answered Nov 09 '22 23:11

OmG


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)
like image 15
Majid Avatar answered Nov 09 '22 22:11

Majid


pip install jdatetime

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)
like image 6
Majid Vafaei Avatar answered Nov 10 '22 00:11

Majid Vafaei