Date&Time

āļŦāļēāļ§āļąāļ™āļ—āļĩāđˆāļ™āļąāļšāđ„āļ›āļˆāļēāļāļ§āļąāļ™āļ—āļĩāđˆāļ›āļąāļˆāļˆāļļāļšāļąāļ™

import datetime as dt


def get_date_from_today(delta, strfmt='%Y%m%d'):
    """ Returns a string that represents a date n numbers of days from today.

    Parameters
    ----------
        delta : int
            number of days
        strfmt : string
            format in which the date will be represented

    """
    return (dt.date.today() + dt.timedelta(delta)).strftime(strfmt)
delta = 30 # āļšāļ§āļāļˆāļēāļāļ§āļąāļ™āļ—āļĩāđˆāļ›āļąāļˆāļˆāļļāļšāļąāļ™āđ„āļ› 30 āļ§āļąāļ™
result = get_date_from_today(delta)

print(result)

# '20200830' [āļ§āļąāļ™āļ—āļĩāđˆāļ›āļąāļˆāļˆāļļāļšāļąāļ™ = 20200731]

String to Datetime

from datetime import datetime
from dateutil.parser import parse

def string_date(text, fmt = "%d/%m/%Y" ):     
    dates = parse(text)
    
    return dates
text = '2020-01-30'
result = string_date(text)

print(result)
type(result)


# 2020-01-30 00:00:00
# datetime.datetime

Datetime to String

def date_string(dates,str_time =  '%d-%m-%Y' ):
    date_string = dates.strftime(str_time)
    
    return  date_string 

# output : '21-08-2020'

Last updated