Days till Christmas How To

This post is a follow-up from ‘Easy Way to Get the Difference of Two Times in Python’, seen here. If confused, please read that first, then come back to this.

 

So, continuing from where we left off, lets say we want to get the number of days till christmas. For that, we’d do the following.


#! /usr/bin/python3

import arrow

class CustomArrow(arrow.Arrow):
    def days_till_xmas(self):
        xmas = arrow.Arrow(self.year, 12, 25)
        if self > xmas:
            xmas = xmas.replace(years=1)

        return (xmas - self).days

factory = arrow.ArrowFactory(CustomArrow)
mytime = factory.now()
print(mytime.days_till_xmas())

And boom, that’s it!

Easy Way to Get the Difference of Two Times in Python

This post is quite the short one, but if you ever need the duration/difference between two times, this can come in very handy. But the code may be confusing. So bear with me.

So if you can get the total amount of seconds, which to make it easier, I used the package ‘arrow’.


pip install arrow
pip3 install arrow

Here’s my code, a more stripped down version than what I use.


#! /usr/bin/python3
import arrow
import datetime
def secondsToText(secs):
    days = secs//86400
    hours = (secs - days*86400)//3600
    minutes = (secs - days*86400 - hours*3600)//60
    seconds = secs - days*86400 - hours*3600 - minutes*60
    result = ("{0} day{1}, ".format(int(days), "s" if days!=1 else "") if days else "") + \
    ("{0} hour{1}, ".format(int(hours), "s" if hours!=1 else "") if hours else "") + \
    ("{0} minute{1}, ".format(int(minutes), "s" if minutes!=1 else "") if minutes else "") + \
    ("{0} second{1}".format(int(seconds), "s" if seconds!=1 else "") if seconds else "")
    return result
my_year  = int(input("Year --> "))
my_month = int(input("Month (01-12) --> "))
my_day   = int(input("Day (01-31) --> "))
my_hour  = int(input("Hour (00-23 --> "))
my_min   = int(input("Minute (00-59) --> "))
my_sec   = int(input("Second (00-59) --> "))
my_datetime = datetime.datetime(my_year, my_month, my_day,
                my_hour, my_min, my_sec)
now = arrow.now("US/Eastern")
mypasttime = my_datetime
then = arrow.get(mypasttime, "US/Eastern")
time_diff = now - then
time_days = time_diff.days
time_diff = secondsToText(time_diff.total_seconds())

print(time_diff)

So lets say I wanted the exact length of time(in days + rest) since ElectroCode began.

And while I know the date was January 20th 2012, I don’t exactly know the exact time, so we’ll use Noon(12 00 00)


[email protected]:~$ ./time.py 
Year --> 2012
Month (1-12) --> 01
Day (1-31) --> 20
Hour (0-23 --> 12
Minute (0-59) --> 00
Second (0-59) --> 00
1692 days, 3 minutes, 9 seconds

Now if I wanted to make that able to parse weeks and years, so now I’ll add a few additions to the secondsToText() method.

Results

So with those changes, I now get

4 years, 33 weeks, 1 day, 1 hour, 11 minutes, 38 seconds

If you think there should be changes to this post, leave a comment down below! I can’t improve if there’s no response of what I need to improve on.

See here for other ways to contact me.


  • If this page or post could use some sprucing up, let us know by contacting us through this link