Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created August 30, 2020 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/bf2d6a6ce3533668386720e0804c40c6 to your computer and use it in GitHub Desktop.
Save amankharwal/bf2d6a6ce3533668386720e0804c40c6 to your computer and use it in GitHub Desktop.
# Contains custom functions for dates and currency values.
import datetime as dt
def to_date(any_str):
try:
if len(any_str) == 10:
the_date = dt.datetime.strptime(any_str,'%m/%d/%Y').date()
else:
the_date = dt.datetime.strptime(any_str,'%m/%d/%y').date()
except (ValueError, TypeError):
the_date = None
return the_date
def mdy(any_date):
if type(any_date) == str:
any_date = to_date(anydate)
if isinstance(any_date,dt.date):
s_date = f"{any_date:'%m/%d/%Y'}"
else:
s_date = "Invalid date"
return s_date
def to_curr(anynum, len=0):
s = "Invalid amount"
try:
x = float(anynum)
except ValueError:
x= None
if isinstance(x,float):
s = '$' + f"{x:,.2f}"
if len > 0:
s=s.rjust(len)
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment