Hello Learners…
Welcome to my blog…
Table Of Contents
- Introduction
- Let’s Start Working With Date And Time In Python
- Get The Date And Of The Current System
- Get The TimeZone In Date And Time
- Get The Date In Python
- Get The Current System Time
- Get The Day From The Date
- Get The Month From The Date
- Get The Year From The Date
- Summary
- References
Introduction
In this post, we learn how to work with date and time using Python. When we are working on data science projects or any other projects then we mostly use the datetime() library or we can say it is a package of Python which helps us to work with date and time. date and time is the most important part of any project.
Let’s Start Working With Date And Time In Python
Python is the basic requirement for this, please check if it is already installed.
datetime() is already installed with Python so we can use it directly. no need to install the module separately.
Get The Date And Time Of The Current System
This will print the date and time of the current system on which we are running the below code.
from datetime import datetime
date_time=datetime.now()
print(date_time)
The output of the above code snippet.
2023-04-29 05:56:41.577043
Get The TimeZone In Date And Time
Here we see the timezone of our current system.
from datetime import datetime
date_time=datetime.now().astimezone()
print(date_time)
The output of the above code snippet.
2023-04-29 06:29:00.590955+05:30
Here +5:30 shows the timezone of the country based on our system.
Get The Date In Python
Here we are trying to get the Date of our current system.
from datetime import datetime
today_date=datetime.now().date()
print(today_date)
The output of the above code snippet.
2023-04-29
Get The Current System Time
Here we are trying to get the Time of our current system.
from datetime import datetime
current_system_time=datetime.now().time()
print(current_system_time)
The output of the above code snippet.
06:38:56.650023
Get The Day From The Date
Here we see how we can extract or get only the day from the full date.
from datetime import datetime
today_date=datetime.now().date()
print(today_date)
current_date_day=datetime.now().date().day
print(current_date_day)
The output of the above code snippet.
#Full Date
2023-04-29
#Day of the date
29
There is also another way to get the day from the date.
from datetime import datetime
today_date=datetime.now().date()
print(today_date)
current_date_day=datetime.now().day
print(current_date_day)
The output of the above code snippet.
#Full Date
2023-04-29
#Day of the date
29
Get The Month From The Date
Here we see how we can extract or get only the month from the full date.
from datetime import datetime
current_date=datetime.now().date()
print(current_date)
current_date_month=datetime.now().date().month
print(current_date_month)
The output of the above code snippet.
# Full Date
2023-04-29
# Month Of The Date
4
There is also another way to get the month from the date.
from datetime import datetime
current_date=datetime.now().date()
print(current_date)
current_date_month=datetime.now().month
print(current_date_month)
The output of the above code snippet.
# Full Date
2023-04-29
# Month Of The Date
4
Get The Year From The Date
Here we see how we can extract or get only the year from the full date.
from datetime import datetime
current_date=datetime.now().date()
print(current_date)
current_date_year=datetime.now().year
print(current_date_year)
The output of the above code snippet.
# Full Date
2023-04-29
# Year Of The Date
2023
There is also another way to get the month from the date.
from datetime import datetime
current_date=datetime.now().date()
print(current_date)
current_date_year=datetime.now().date().year
print(current_date_year)
The output of the above code snippet.
# Full Date
2023-04-29
# Year Of The Date
2023
Summary
These all are simple methods to use and we can use the datetime module for our use cases using all this we can create a great project on how we can use the datetime module that we can see in upcoming posts.
Also, you can read more articles,