Dates in Python

by | Sep 24, 2018 | Python-example | 0 comments

Create date from a StringPython logo

import pandas as pd
startdate = "10/10/2018"
my_date = pd.to_datetime(startdate)
print(my_date.strftime("%Y-%m-%d"))
2018-10-10

Create current date

import datetime
my_date = datetime.datetime.now()
print(my_date.strftime("%Y-%m-%d"))
2018-10-10

Increase days

enddate = my_date + pd.DateOffset(days=5)
print(enddate.strftime("%Y-%m-%d"))
2018-10-15

Decrease days

enddate = my_date - pd.DateOffset(days=5)
print(enddate.strftime("%Y-%m-%d"))
2018-10-05

Pass date to Numeric: Unit timestamp

print("Unix Timestamp: ", (time.mktime(my_date.timetuple())))
1538690400.0

0 Comments

Submit a Comment

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