Skip to content

Instantly share code, notes, and snippets.

@maniartech
Created November 10, 2012 15:09
Show Gist options
  • Save maniartech/4051358 to your computer and use it in GitHub Desktop.
Save maniartech/4051358 to your computer and use it in GitHub Desktop.
Contains useful datetime related functions...
import datetime as dt
from datetime import datetime
import pytz
def local_datetime(timezone = 'Asia/Calcutta'):
"""
Returns the local time based on provided timezone.
"""
utc = datetime.utcnow()
tz = pytz.timezone(timezone)
return tz.fromutc (utc)
def add_days(date_time, days):
"""
Add the new date after adding specified numbers of days to provided date_time.
"""
return date_time + dt.timedelta(days)
def add_months(date_time, months):
"""
Add the new date after adding specified numbers of months to provided date_time.
"""
return date_time + dt.timedelta(months*365/12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment