MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
October
Sun Mon Tue Wed Thu Fri Sat
7
       
2017
Months
Oct


Sat, Oct 07, 2017 10:59 pm

Determining the day of year value using Python

I sometimes need to determine the day of the year corresponding to today's date. The day of year starts with January 1 as day 1 and for 2017, December 31 is day 365. You can find sites online that will provide those values, e.g. Day Numbers for 2017 or NOAA's DOY Calendar. Or, on a system with Python installed, e.g., Linux or OS X, you can use the datetime module to obtain the day of the year corresponding to the current date as shown below:

$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.today().timetuple().tm_yday
280
>>> exit()
$

Since today is October 7 of the year 2017, the day of the year is 280.

If you want to obtain the day of year (DOY) for another date, you can use datetime.date(year, month, day).timetuple().tm_yday where year is the relevant year, month is the month, and day is the day of interest. E.g., March 1, 2017 is DOY 60:

>>> import datetime
>>> datetime.date(2017, 3, 1).timetuple().tm_yday
60
>>>

[/languages/python] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo