[Tutor] Finding numeric day in a year...

Alan Gauld alan.gauld at btinternet.com
Sat Jun 28 20:39:20 CEST 2014


On 28/06/14 18:59, Ken G. wrote:

> datecode = "20140101" # from database on file
>
> month = datecode[4:6]
> day  = datecode[6:8]
> year  = datecode[0:4]

use strptime() to parse dates, its much more reliable.

> datecode = year + "-" + month + "-" + day
> today = datecode

And use strftime() to format them...

> print today
> print
>
> print "Day of year: ", datetime.date.today().strftime("%j")

This returns todays date whenever you run it.
It has nothing to do with the dates above.
But if you use strptime() to get the date from your string you should 
then be able to use strftime to convert it to julian.

BTW You say you get it from "database on file".
Now if that is a real database such as SQLite you will find functions 
there to convert it to julian at source... which is easier than reading 
it as a string, parsing it, and then converting it back to a date again...


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list