Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What's the difference between from datetime and normal import datetime?


Asked by Sariah Berg on Dec 02, 2021 FAQ



Note also that, if you do import the module name itself, you can shorten the name to ease typing: The difference between from datetime import datetime and normal import datetime is that , you are dealing with a module at one time and a class at other.
And,
Python datetime Unlike other programming languages, Python doesn’t allow us to work with Date objects directly. To work with date and time objects, you have to import a module called datetime in Python. This Python datetime module holds different classes to work with dates or to manipulate dates and times.
Keeping this in consideration, The datetime data type has a fixed storage size of 8 bytes. The datetime2 on the other hand, can be either 6, 7, or 8 bytes, depending on its precision. When using 3 decimal places, datetime2 uses just 7 bytes, which means it uses less storage space than datetime (with more accuracy).
Also Know,
datetime.datetime is a combination of a date and a time. It has all the attributes of both classes. The three classes that represent dates and times in datetime have similar initializers. They can be instantiated by passing keyword arguments for each of the attributes, such as year, date, or hour.
Additionally,
In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime in Python are the objects, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.