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
11 ответов
+ 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
+ 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()>
+ 3
you are welcome!
+ 3
Akhil, here is a code that i prepared today for someone else:
https://code.sololearn.com/cx5QUGWHTFK8/?ref=app
+ 3
Akhid. i will put this placeholders together and post it
+ 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
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?
0
Lothar
Thanks mate!
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
0
Thanks Lothar ..
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