Hello I every can any one tell me exactly how to extract date and time in python I am taking a course and I keep getting track back errors on line 4 but I am FOLLOWING INSTRUCTIONS TO A T HELP!!!
i think this will help #!/usr/bin/python import time; # This is required to include time module. ticks = time.time() print "Number of ticks since 12:00am, January 1, 1970:", ticks
what is ur code?
from datetime import datetime print datetime.now current_month, day, year = now.date print now.date()
trying to get the month day and year
import datetime today = datetime.datetime.now() day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1
2012-10-18 00:35:17.066445 Oops, try again. Make sure that you're using the variable now to grab the parts of the date
thats the error message I got
I don't think I understand what there telling me to do heres the instructions printing out mm/dd/yyyy hh:mm:ss, let's tackle adding / slashes to the date's parts. You might think to do something like: print now.month, "/", now.day, "/", now.year However, this would give you spaces between the slashes. Hence, the better solution is to use string concatenation (the + operator), covered in Unit 2. As you'll see, it's not as simple as just using concatenation -- mainly because concatenation only works with strings. When you extract information like now.year, you end up with an integer. To convert an integer to a string, you can use the str() function. For example, if a variable x had the value 4 and we wanted to convert that into "4", you could do str(x).
check this one also out: import datetime now = datetime.datetime.now() print print "Current date and time using str method of datetime object:" print str(now) print print "Current date and time using instance attributes:" print "Current year: %d" % now.year print "Current month: %d" % now.month print "Current day: %d" % now.day print "Current hour: %d" % now.hour print "Current minute: %d" % now.minute print "Current second: %d" % now.second print "Current microsecond: %d" % now.microsecond print print "Current date and time using strftime:" print now.strftime("%Y-%m-%d %H:%M")
They're just using now.day, now.minute, etc. to teach about combining multiple values in an output. You want to output a string so that you can control the formatting (no spaces). If you need to turn multiple numbers (a,b,c) into an output, first turn them into strings (str(a), str(b), str(c)) and then concatenate them with any other strings of symbols you need in the answer (str(a)+':'+str(b)+':'+str(c)).
Join our real-time social learning platform and learn together with your friends!