>________________________________
> From: Ken G. <[email protected]>
>To: [email protected] 
>Sent: Saturday, June 28, 2014 7:59 PM
>Subject: [Tutor] Finding numeric day in a year...
> 
>
>
>I know the correct answer should be 001, but I keep getting 179 which is the 
>correct answer for June 28, 2014 (I think). I tried using datecode in various 
>places instead of today but I am still getting 179. Currently using Ubuntu 
>12.04.4 and Python 2.7. Thanks for any feedback and suggestion.
>
>
>PROGRAM DISPLAY:
>
># datefind 03.py
>
>import datetime
>
>datecode = "20140101" # from database on file
>
>month = datecode[4:6]
>day  = datecode[6:8]
>year  = datecode[0:4]
>datecode = year + "-" + month + "-" + day
>today = datecode
>print today
>print
>
>print "Day of year: ", datetime.date.today().strftime("%j")
>
>
>TERMINAL DISPLAY:
>
>2014-01-01
>
>Day of year:  179
>
>------------------
>(program exited with code: 0)
>Press return to continue

Hello,

Your variable 'today' and the datetime function 'today()' are not the same 
thing. You need strptime to parse the date string (and tell it the format is 
yyyymmdd), then strftime to format it using the day-of-year format '%j' 

>>> datetime.datetime.strptime(datecode, "%Y%m%d").strftime("%j")
'001'
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to