Why only "import datetime" didn't work? Why I had to show "from datetime import datetime" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why only "import datetime" didn't work? Why I had to show "from datetime import datetime"

Without showing the code "from datetime" import datetime didn't work. It showed that datetime doesn't have an attribute "today". I remember while playing around the code on SL somewhere i didn't use from but only import did work there (I don't remember which one) Your answer will help this inquisitive learner. Thank you. https://code.sololearn.com/ca8DiLW2cLrF/?ref=app

22nd May 2019, 7:29 AM
Akhil Padmanabhan
Akhil Padmanabhan - avatar
11 Answers
+ 5
Table with datetime strftime() placeholders. You need an appropriate screen size. Don‘t try it on your smart phone ;-)) https://code.sololearn.com/cwgxS93LzVoF/?ref=app
24th May 2019, 4:26 PM
Lothar
Lothar - avatar
+ 4
Hi, using date or time calculations and handling needs to import the module <datetime>. This module provides classes to handle dates, so you call say: <from datetime import date> but this class is only for date handling, not for time. <from datetime import time> is the same principle but only works for time handling. You can check this by trying <datetime.time.> so you get only time functions offered. Now besides datetime module there’s also a class named datetime that has both date and time functionality. Try it by using <from datetime import datetime> and then <datetime.datetime.today()>
22nd May 2019, 9:12 AM
Lothar
Lothar - avatar
+ 3
you are welcome!
23rd May 2019, 9:47 AM
Lothar
Lothar - avatar
+ 3
Akhil, here is a code that i prepared today for someone else: https://code.sololearn.com/cx5QUGWHTFK8/?ref=app
24th May 2019, 12:34 PM
Lothar
Lothar - avatar
+ 3
Akhid. i will put this placeholders together and post it
24th May 2019, 1:26 PM
Lothar
Lothar - avatar
+ 2
Akhil, if you say "import datetime" you import the module "datetime". Module datetime has several classes like date, time, timezone,... , datetime.. So this class has the same name as the module. I don't know why they did it this way - normally you should not! So some people do the import as: "import datetime as dt". Then you can call the datetime class: "dt.datetime.today()". But it's anyway confusing. So it' not a submodule as you suspect. You can explore these things with the help function. To have a good overview, you need to do this from a regular pc or from a tablet with an appropriate screen size. Everything you do from now is to use the console. Start importing "import datetime". Then type "help(datetime)" to see whats inside module datetime. You will see in text mode an overview of all classes that belong to module datetime. You can also see methods and properties from each class. Next step is to call help for the class datetime: "help(datetime.datetime)". So you have now an overview on this class with methods, properties,... Hope this helps
22nd May 2019, 4:23 PM
Lothar
Lothar - avatar
0
Lothar So by just having import datetime I'm actually importing a class and not a submodule of the module datetime which i would have been doing if i used from datetime import datetime?
22nd May 2019, 10:51 AM
Akhil Padmanabhan
Akhil Padmanabhan - avatar
0
Lothar Thanks mate!
23rd May 2019, 6:53 AM
Akhil Padmanabhan
Akhil Padmanabhan - avatar
0
Lothar I went through help(datetime) and found this ctime which is quite close. I suppose ctime is current time. But i couldn't find how it's %a for Fri and %b for May.. For this I tried help(datetime.datetime.today().strftime) (haha yea I did) but didn't find anything. Please tell me more about this.. https://code.sololearn.com/c2I8VP3qH7Vr/?ref=app
24th May 2019, 1:16 PM
Akhil Padmanabhan
Akhil Padmanabhan - avatar
0
Thanks Lothar ..
24th May 2019, 1:43 PM
Akhil Padmanabhan
Akhil Padmanabhan - avatar
0
Thank you so much.. You're so sweet to lut this together.. this makes less sense to me though like everything was so obvious till now like to print the command is print (how convenient).. I'm learning I'm sorry haha
25th May 2019, 3:49 AM
Akhil Padmanabhan
Akhil Padmanabhan - avatar