Search This Blog

Friday, March 22, 2019

Python datetime operations


Python has a module named datetime to work with date and time.

Below are few examples of various datetime operations in python.

import time
import datetime

print("Time in seconds: %s" %time.time())

print("Current date_time1: " , datetime.datetime.now()

print("Current date_time2: " ,datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))

print("Current year: ", datetime.date.today().strftime("%Y"))

print("Current month: ", datetime.date.today().strftime("%B"))

print("Current week number: ", datetime.date.today().strftime("%W"))

print("Weekday of the week: ", datetime.date.today().strftime("%w"))

print("Day of year: ", datetime.date.today().strftime("%j"))

print("Day of the month: ", datetime.date.today().strftime("%d"))

print("Day of week: ", datetime.date.today().strftime("%A"))

Output:

Time in seconds: 1553207598.3700132
Current date_time1:  2019-03-21 22:33:18.370013
Current date_time2:  19-03-21-22-33
Current year:  2019
Current Month:  March
Week number of the year:  11
Weekday of the week:  4
Day of year:  080
Day of the month:  21
Day of week:  Thursday

No comments:

Post a Comment