Showing posts with label in python. Show all posts
Showing posts with label in python. Show all posts

Tuesday, February 1, 2022

Date and time in python using datetime modules

 



import datetime

year_number=2021
month_number=12
date_number=27
hours_number=15
minutes_number=50
seconds_number=30

datetime.datetime(year_number, month_number, date_number, hours_number, minutes_number, seconds_number)



# =============================================================================
#
# =============================================================================

import datetime

print (datetime.date.today())


# =============================================================================
#
# =============================================================================

from datetime import date

print (date.today())





# =============================================================================
#
# =============================================================================

from datetime import date

print (datetime.date.today().day)

print (datetime.date.today().month)

print (datetime.date.today().year)

#If you prefer to write in one sentence, write it as

print (datetime.date.today().day,datetime.date.today().month,datetime.date.today().year)




# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.day)

print (now.month)

print (now.year)

#You can also display the previous 3 sentences in one sentence

print (now.day, now.month, now.year)

#This code is cleaner and compact. Try to write the code in shortest way possible without affecting readability.


# =============================================================================
#
# =============================================================================

from datetime import date

now = date.today()

print (now.weekday())



# =============================================================================
#
# =============================================================================

Today = datetime.now()

print (Today.strftime("%a, %B, %d, %y"))





# =============================================================================
#
# =============================================================================

import datetime

Today = datetime.now()

print ("Today’s date is" ,Today.strftime("%c"))



# =============================================================================
#
# =============================================================================

from datetime import date,time,datetime,timedelta

Time_gap = timedelta(hours = 23, minutes = 34)
#Time_gap is the timedelta object where we can do different calculations on it.

print ("Future time is ", str(datetime.now() + Time_gap))



python class topic video