Skip to content

Latest commit

 

History

History
66 lines (40 loc) · 1.97 KB

Python_Using_datetime_library.md

File metadata and controls

66 lines (40 loc) · 1.97 KB



Template request | Bug report | Generate Data Product

Tags: #python #datetime #snippet #operations

Author: Florent Ravenel

Description: This notebook provides an introduction to using the Python datetime library to work with dates and times.

Input

Import library

from datetime import datetime

Setup Variables

# Date string format
date_string = "2022-02-25"

# Your date string format
current_format = "%Y-%m-%d"

# New date format you want to use
new_format = "%Y-W%U"

Model

Convert a string date to a datetime object

date_datetime = datetime.strptime(date_string, current_format)
date_datetime

Convert a datetime object to string date

new_date = date_datetime.strftime(new_format)
new_date

Convert a string date to a datetime object to another string date

new_date = datetime.strptime(date_string, current_format).strftime(new_format)
new_date

Output

Display new date

new_date