Package: date-time-literal
date_literalA package/module for converting date or date-time formats to literal days, hours, minutes, or seconds for comparison or whatever.
For example, you can convert DateTime objects to know just how much of a difference there is between them, or you just want
to get the human-readable format of DateTime or Date objects.Installationpip install date_time_literalUsageIMPORTANTDateTime or Date objects must be in the default 'Y-M-D Hr:Min:Sec' and 'Y-M-D' formats respectively.Converts Date Time Literalfromdjango.utilsimporttimezonetime=timezone.now()fromdate_time_literalimportConvertTimeconvert_time=ConvertTime(time).convert_timeConverts Date Literalconvert_time = ConvertTime(time).convert
This returns the date or date_time in seconds. You can add an optional parameter to specify what you want.Note: You can use the convert class function on a DateTime object if you wish to use only the date part of the DateTime object,
but cannot use the convert_time class function on a Date object, only on DateTime objects.Specific conversion to daysfromdjango.utilsimporttimezonefromdatetimeimportdatetimefromdate_time_literalimportConvertTimetime2=datetime.now()time=timezone.now()convert_time=ConvertTime(time,'d').convert_timeconvert_time2=ConvertTime(time2,'d').convertSpecific conversion to hoursfromdate_time_literalimportConvertTimefromdatetimeimportdatetimetime=datetime.now()convert_time=ConvertTime(time,'d').convert_time# Converts the DateTime object to daysconvert_time1=ConvertTime(time,'h').convert_time# Converts the DateTime object to hoursconvert_time2=ConvertTime(time,'m').convert_time# Converts the DateTime object to minutes# The default conversion if none is specified is to secondsCHECK DATE-TIME OR DATE DIFFERENCE BETWEEN TWO DATE-TIME OR DATE OBJECTSfromdate_time_literalimportConvertTimefromdatetimeimportdatetimefromdjango.utilsimporttimezonetime=datetime.now()convert_time=ConvertTime(time,'h').convert_timefromdate_time_literalimportdate_time_diff,date_diffconvert_time2=ConvertTime(time,'m').convert_timedate1='2021-05-31'date2='2021-03-21'date_time1=timezone.now()date_time2='2021-03-21 23:16:45.735963'date_l=date_diff(date1,date2,'d')date_time_l=date_time_diff(date_time1,date_time2,'d')# date_time_l will return the difference in value between date_time1 and date_time2 in days. You can use the# corresponding string literal to get for minutes, hours and seconds which is the default value.# date_time_diff returns the difference in two dateTimes to the second. This should be used when you need to get# date time difference to the last secondConvert between time and date objectsYou can also use the convert_time function to convert between timesfromdate_time_literalimportconvert_time# Converts between years, days, hours, minutes, and secondsprint(convert_time(365,'d','y'))# Converts 365 days to yearsprint(convert_time(5,'y','d'))# Converts 5 years to daysprint(convert_time(5,'y','h'))# Converts 5 years to hoursprint(convert_time(5,'d','s'))# Converts 5 days to seconds# etcSome Basic use casesTo get a rather comprehensive idea of how the package works, copy the code below and run it. The convert_time function
was updated to be more efficient and now allows for week conversion as well in version 1.0.8fromdate_time_literalimportConvertTime,DateDiff,date_diff,convert_time,date_time_difffromdatetimeimportdatetimet=ConvertTime('2021-05-31 23:16:55.321568','d')p=ConvertTime(datetime.now(),'d')i=ConvertTime('2021-05-31 23:16:55+00:00','d')d=ConvertTime('2021-05-31',time_format='d')e=DateDiff('2021-05-31','2021-04-31','d')print(t.convert_time)print(i.convert_time)print(d.convert)print(e.date_diff)print(date_diff('2021-08-31','2021-05-30','D'))print(date_time_diff('2021-04-30 21:58:50+00:00','2021-04-30 10:58:55+00:00','H'))print(convert_time(1,"y",'h'))print(convert_time(365,'d','y'))