Skip to content

Instantly share code, notes, and snippets.

@Pradip-p
Created February 18, 2022 04:49
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 Pradip-p/2082be7c21c2a40af17c192a695174a2 to your computer and use it in GitHub Desktop.
Save Pradip-p/2082be7c21c2a40af17c192a695174a2 to your computer and use it in GitHub Desktop.
get get_current_date,get_day,get_month,get_year and get_time in python
import datetime
def get_current_date(format="%Y-%m-%d"):
"""
Returns the current date
:param format: desired Date format
examples:
%Y-%m-%d
%d-%m-%Y
%Y/%m/%d
:return: date string in the format specified.
Default format is YYYY-MM-DD
"""
return datetime.datetime.now().strftime(format)
def get_day():
"""
returns current day
:return: int
"""
return datetime.datetime.now().day
def get_month():
"""
returns current month
:return: int
"""
return datetime.datetime.now().month
def get_year():
"""
Returns current year
:return: int
"""
return datetime.datetime.now().year
def get_time(format='%H:%M:%S'):
"""
Returns current time
:param format :desired time format
default = %H:%M:%S
examples:
%H-%M-%S
%H:%M:
:return: int
"""
return datetime.datetime.now().strftime(format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment