-
-
Save amankharwal/bf2d6a6ce3533668386720e0804c40c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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