import datetime

# get the current date and time
current_datetime=datetime.datetime.now()

# print the current date and time
print(“Current date and time:”, current_datetime)

Output:

Current date and time: 2025-02-21 10:49:33.046932

datetime.date.today()
datetime.date(2025, 2, 21)

datetime.datetime(2001,1,31,10,51,0)
datetime.datetime(2001, 1, 31, 10, 51)

mydatetime=datetime.datetime.fromtimestamp(528756281)
mydatetime
datetime.datetime(1986, 10, 4, 2, 14, 41)

mydatetime.timestamp()
528756281.0

Example 1:
———-
import datetime
d=datetime.date(2022, 2, 17)
print(d)

Output:

2022-02-17

***Next***

from datetime import date
a=date(2019, 4, 13)
print(a)
2019-04-13

Example – 2:

from datetime import date
today=date.today()
print(“Current date=”, today)

Output:

Current date= 2025-02-21

Example 3:

from datetime import date
timestamp=date.fromtimestamp(1326244364)
print(“Date=”, timestamp)

Output:

Date= 2012-01-11

Example : – 4:

import datetime
date_object=datetime.date.today() # create a date object for todays date
# print the date object
print(“Date object:”, date_object)
# print the year, month, day, and weekday from the date object
print(“Year:”, date_object.year)
print(“Month:”, date_object.month)
print(“Day:”, date_object.day)
print(“Weekday:”, date_object.weekday()) # Monday is 0 and Sunday is 6

Output:

Date object: 2025-02-21
Year: 2025
Month: 2
Day: 21
Weekday: 4

import datetime
# create a time object for 9:30:45.678 using datetime.time(9, 30, 45, 678000)
time_object = datetime.datetime.now().time() # create a time object for present time
# print the time object
print(“Time object:”, time_object)
# print the hour, minute, second, and microsecond attributes
print(“Hour:”, time_object.hour)
print(“Minute:”, time_object.minute)
print(“Second:”, time_object.second)
print(“Microsecond:”, time_object.microsecond)

OUTPUT :

Time object: 15:28:13.486721
Hour: 15
Minute: 28
Second: 13
Microsecond: 486721

Next:

import datetime
# create a datetime object for February 17, 2023 at 9:30:45.678
datetime_object = datetime.datetime(2023, 2, 17, 9, 30, 45, 678000)
#print the datetime object
print(“Datetime object:”, datetime_object)
# print the year, month, day, hour, minute,
# second, microsecond and timestamp attributes
print(“Year:”, datetime_object.year)
print(“Month:”, datetime_object.month)
print(“Day:”, datetime_object.day)
print(“Hour:”, datetime_object.hour)
print(“Minute:”, datetime_object.minute)
print(“Second:”, datetime_object.second)
print(“Microsecond:”, datetime_object.microsecond)
print(“timestamp:”, datetime_object.timestamp())

OUTPUT :

Datetime object: 2023-02-17 09:30:45.678000
Year: 2023
Month: 2
Day: 17
Hour: 9
Minute: 30
Second: 45
Microsecond: 678000
timestamp: 1676606445.678

Next:

import datetime
# create a timedelta object representing 2 days, 3 hours, and 30 minutes
delta_object = datetime.timedelta(days=2, hours=3, minutes=30)
# create a datetime object for February 17, 2023 at 9:30:45
datetime_object = datetime.datetime(2023, 2, 17, 9, 30, 45)
# add the timedelta object to the datetime object
new_datetime_object = datetime_object + delta_object
# subtract the timedelta object from the datetime object
old_datetime_object = datetime_object – delta_object
# print the results
print(“Original datetime:”, datetime_object)
print(“New datetime:”, new_datetime_object)
print(“Old datetime:”, old_datetime_object)

OUT PUT:

Original datetime: 2023-02-17 09:30:45
New datetime: 2023-02-19 13:00:45
Old datetime: 2023-02-15 06:00:45

Next :

import datetime
# create two datetime objects
start_time = datetime.datetime(2023, 2, 17, 9, 30, 0)
end_time = datetime.datetime(2023, 2, 20, 17, 30, 0)
# calculate the difference between the two datetime objects
time_dalta = end_time – start_time
# create two timedelta objects
delta1 = datetime.timedelta(hours=1)
delta2 = datetime.timedelta(days=2, hours=3)
# calculate the difference between two timedelta objects
delta3 = delta2 – delta1
#print the results
print(“Time difference:”, time_delta)
print(“Delta difference:”, delta3)

OUT PUT:
Next :

import datetime
# create a current datetime object
dt = datetime.datetime.now()
# format the datetime object as “YYYY-MM-DD”
date_string = dt.strftime(“%Y-%m-%d”)
# format the datetime object as “DD/MM/YYYY”
date_string2 = dt.strftime(“%d/%m/%Y”)
# format the datetime object as “YYYY-MM-DD HH:MM:SS”
datetime_string = dt.strftime(“%Y-%m-%d %H:%M:%S”)
# print the results
print(date_string)
print(date_string2)
print(datetime_string)

Out Put :

2025-02-15
15/02/2025
2025-02-15 15:33:01

Next :

import datetime
# create a string representing the date and time
date_string = “2023-02-17 09:30:45”
# parse the string into a datetime object
dt = datetime.datetime.strptime(date_string, “%Y-%m-%d %H:%M:%S”)
# print the datetime object
print(dt)

OUT PUT :

2023-02-17 09:30:45

Next :

import datetime
# Enter your birthdate
birthdate = datetime.date(1989, 11, 27)
# Calculate the number of days since birthdate
days_since_birth = (datetime.date.today() – birthdate).days
# Print the result
print(“It has been {} days since your birthdaye.”.format(days_since_birth))

OUT PUT :

It has been 12864 days since your birthdaye.

Next :

import datetime
# Define the start and end dates
start_date = datetime.date(2022, 1, 1)
end_date = datetime.date(2022, 12, 31)
# Initialize a counter variable
num_saturdays = 0
# Iterate over the days between the start and end dates
for d in range((end_date – start_date).days + 1):
date = start_date + datetime.timedelta(d)
# Check if the current day is a Saturday
if date.weekday() == 5:
num_saturdays +=1
# Print the result
print(“There are {} Saturdays between {} and {}.”. format(num_saturdays, start_date, end_date))

OUT PUT :

There are 1 Saturdays between 2022-01-01 and 2022-12-31.
There are 2 Saturdays between 2022-01-01 and 2022-12-31.
There are 3 Saturdays between 2022-01-01 and 2022-12-31.
There are 4 Saturdays between 2022-01-01 and 2022-12-31.
There are 5 Saturdays between 2022-01-01 and 2022-12-31.
There are 6 Saturdays between 2022-01-01 and 2022-12-31.
There are 7 Saturdays between 2022-01-01 and 2022-12-31.
There are 8 Saturdays between 2022-01-01 and 2022-12-31.
There are 9 Saturdays between 2022-01-01 and 2022-12-31.
There are 10 Saturdays between 2022-01-01 and 2022-12-31.
There are 11 Saturdays between 2022-01-01 and 2022-12-31.
There are 12 Saturdays between 2022-01-01 and 2022-12-31.
There are 13 Saturdays between 2022-01-01 and 2022-12-31.
There are 14 Saturdays between 2022-01-01 and 2022-12-31.
There are 15 Saturdays between 2022-01-01 and 2022-12-31.
There are 16 Saturdays between 2022-01-01 and 2022-12-31.
There are 17 Saturdays between 2022-01-01 and 2022-12-31.
There are 18 Saturdays between 2022-01-01 and 2022-12-31.
There are 19 Saturdays between 2022-01-01 and 2022-12-31.
There are 20 Saturdays between 2022-01-01 and 2022-12-31.
There are 21 Saturdays between 2022-01-01 and 2022-12-31.
There are 22 Saturdays between 2022-01-01 and 2022-12-31.
There are 23 Saturdays between 2022-01-01 and 2022-12-31.
There are 24 Saturdays between 2022-01-01 and 2022-12-31.
There are 25 Saturdays between 2022-01-01 and 2022-12-31.
There are 26 Saturdays between 2022-01-01 and 2022-12-31.
There are 27 Saturdays between 2022-01-01 and 2022-12-31.
There are 28 Saturdays between 2022-01-01 and 2022-12-31.
There are 29 Saturdays between 2022-01-01 and 2022-12-31.
There are 30 Saturdays between 2022-01-01 and 2022-12-31.
There are 31 Saturdays between 2022-01-01 and 2022-12-31.
There are 32 Saturdays between 2022-01-01 and 2022-12-31.
There are 33 Saturdays between 2022-01-01 and 2022-12-31.
There are 34 Saturdays between 2022-01-01 and 2022-12-31.
There are 35 Saturdays between 2022-01-01 and 2022-12-31.
There are 36 Saturdays between 2022-01-01 and 2022-12-31.
There are 37 Saturdays between 2022-01-01 and 2022-12-31.
There are 38 Saturdays between 2022-01-01 and 2022-12-31.
There are 39 Saturdays between 2022-01-01 and 2022-12-31.
There are 40 Saturdays between 2022-01-01 and 2022-12-31.
There are 41 Saturdays between 2022-01-01 and 2022-12-31.
There are 42 Saturdays between 2022-01-01 and 2022-12-31.
There are 43 Saturdays between 2022-01-01 and 2022-12-31.
There are 44 Saturdays between 2022-01-01 and 2022-12-31.
There are 45 Saturdays between 2022-01-01 and 2022-12-31.
There are 46 Saturdays between 2022-01-01 and 2022-12-31.
There are 47 Saturdays between 2022-01-01 and 2022-12-31.
There are 48 Saturdays between 2022-01-01 and 2022-12-31.
There are 49 Saturdays between 2022-01-01 and 2022-12-31.
There are 50 Saturdays between 2022-01-01 and 2022-12-31.
There are 51 Saturdays between 2022-01-01 and 2022-12-31.
There are 52 Saturdays between 2022-01-01 and 2022-12-31.
There are 53 Saturdays between 2022-01-01 and 2022-12-31.

Next :

import datetime
# Enter your birthdate and the current year
birthdate = datetime.date(1989, 11, 27)
current_year = datetime.date.today().year
# Calculate the date of your next birthday
next_birthday = datetime.date(current_year, birthdate.month, birthdate.day)
if next_birthday < datetime.date.today():
next_birthday == datetime.date(current_year + 1, birthdate.month, birthdate.day)
# Calculate the number of days untill your next birthday
days_left = (next_birthday – datetime.date.today()).days
# Print the result
print(“There are {} days left untill your next birthday.”.format(days_left))

Out Put :

There are 285 days left untill your next birthday.

Next :

import datetime
bdate = datetime.date(1989, 11, 27)
td = datetime.date.today() – bdate
print(td.total_seconds())

OUT PUT :

1111449600.0

Next :

import datetime
date_str = “2022-02-17”
date_obj = datetime.datetime.strptime(date_str, “%Y-%m-%d”)
formatted_date = date_obj.strftime(“%b-%d, %Y”)
print(formatted_date)

Out Put :

Feb-17, 2022

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

FAQs:

 

 

 

 

 

 

 

 

2 thoughts on “DATETIME PACKAGE”

Leave a Reply

Your email address will not be published. Required fields are marked *