A brief note about my day in the life posts (boring, but possibly interesting use of Python datetime)

[Contact Me] | [FAQ]

[Some "Dougisms" Defined]

[About Dickens of a Blog]

[Jump to Site Links]

Summary: Just pointing out something that you may have not noticed about my day in the life posts, some very short code to find out how many days you have been alive using Python.

BLOT: (13 Jul 2012 - 02:16:49 PM)

A brief note about my day in the life posts (boring, but possibly interesting use of Python datetime)

Something I'm not sure if anyone has ever noticed, but a lot of my blog posts have stopped being about my personal life, unless it includes segments of my personal life folded into some other topic—e.g., background to discuss programming or gaming. However, about once every X time units (days, weeks, months), I will release a "Day in the Life" post that is a lot more rambly, and a lot more focused on my personal life. These usually have a number attached. Note sure if anyone has noticed, but said number is actually supposed to be which day in my life that I was currently on at the time of posting. For instance, the most recent one was about the twelve-thousandth, eight-hundreth, and twenty-seventh day that I was alive.

The first few times I used this gimmick/measure, I did the calculations by hand and head, though eventually I developed a short bit of Python code to solve it [part of the code I found online, but that has been some time back and do not remember the source or what their original code did]:

#!/usr/bin/python import datetime YEAR = 1977 MONTH = 05 DAY = 30 d = "%Y/%m/&d" today = datetime.date.today() birth = datetime.date(YEAR, MONTH, DAY) daysinlife = today - birth print daysinlife.days

Feel free to run that if you want to see how many days you have been alive. Just change YEAR, MONTH, DAY to your birthdate, save it as something like "daysalive.py", and there you go. You can also do a number of tasks with minor changes. You can take a second date rather than "today" as the current standard. You can get your age (or a period) in seconds by swapping out .days for .seconds. Here is the documentation of the datetime module. Go ahead...go a little wild.

Python

OTHER BLOTS THIS MONTH: July 2012


Written by Doug Bolden

For those wishing to get in touch, you can contact me in a number of ways

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

The longer, fuller version of this text can be found on my FAQ: "Can I Use Something I Found on the Site?".

"The hidden is greater than the seen."