import datetime
Generate date string
dateStr = datetime.datetime.now().strftime('%Y-%m-%d') # today's date is '2013-05-25'
dateStr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') # today's date is '2013-05-25 16:58:18'
dateStr = datetime.datetime.now().strftime('%Y-%m-%d %I:%M:%S %p') # today's date is '2013-05-25 04:58:18 pm'
dateStr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # today's date is '2016-11-17 06:21:49.251759'
Parse date string
dateTimeObject = datetime.datetime.strptime('3/6/13', "%m/%d/%y")
dateTimeObject = datetime.datetime.strptime('3/6/2013', "%m/%d/%Y")
%w -- 0 is Sunday; 6 is Saturday
%A -- Friday, etc.
For simple time diff output, just do this:
dateTimeStart = datetime.datetime.strptime('3/6/13', "%m/%d/%y")
dateTimeEnd = datetime.datetime.strptime('3/7/13 13:45:55', "%m/%d/%y %H:%M:%S")
the_time_delta = dateTimeEnd - dateTimeStart
type(the_time_delta)
--> <type 'datetime.timedelta'>
print str(the_time_delta)
--> 1 day, 13:45:55
str = '2017-07-04 23:47:56.130'
theDate = datetime.datetime.striptime(str, '%Y-%m-%d %H:%M:%S.%f')
theStart = datetime.datetime.strptime('2017-07-04 23:00:00', '%Y-%m-%d %H:%M:%S')
theEnd = datetime.datetime.strptime('2017-07-05 00:00:00', '%Y-%m-%d %H:%M:%S')
>>> theEnd > theDate >= theStart
True
import calendar
first_weekday, total_days = calendar.monthrange(2018, 2) # 3, 28, ie, Wed and with 28 days
first_weekday, total_days = calendar.monthrange(2020, 2) # 5, 29, ie, Fri and with 28 days
calendar.isleap(2020) # True
# future date by number of days
import datetime
start_date = datetime.datetime.strptime('12/28/18', "%m/%d/%y")
the_delta = datetime.timedelta(days=count)
target_date = start_date + the_delta
import datetime
years = range(1996, 2018)
for year in years:
year_str = str(year)
date_str = year_str + "-1-1"
the_date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
print_date_str = the_date.strftime('%Y-%m-%d %A')
print(print_date_str)
1996-01-01 Monday 1997-01-01 Wednesday 1998-01-01 Thursday 1999-01-01 Friday 2000-01-01 Saturday 2001-01-01 Monday 2002-01-01 Tuesday 2003-01-01 Wednesday 2004-01-01 Thursday 2005-01-01 Saturday 2006-01-01 Sunday 2007-01-01 Monday 2008-01-01 Tuesday 2009-01-01 Thursday 2010-01-01 Friday 2011-01-01 Saturday 2012-01-01 Sunday 2013-01-01 Tuesday 2014-01-01 Wednesday 2015-01-01 Thursday 2016-01-01 Friday 2017-01-01 Sunday
import datetime
# '18-10'
date_str = None
start_day = 8
end_day = 7
def print_date_str(the_date):
str = the_date.strftime('%y-%m-%d (%a)')
print("FFF MB vs. Me MB {date_str}".format(date_str=str))
if not date_str:
date_str = datetime.datetime.now().strftime('%y-%m')
date_str = date_str + '-' + str(start_day)
the_date = datetime.datetime.strptime(date_str, '%y-%m-%d')
count = 0
while True:
the_delta = datetime.timedelta(days=count)
target_date = the_date + the_delta
if target_date.weekday() in [1, 5] or target_date.day == end_day:
print_date_str(target_date)
if target_date.day == end_day:
break
count += 1
#!env python3
import datetime
# '19-02'
date_str = None
start_day = 8
end_day = 7
def print_date_str(the_date):
print_date_str.count += 1
theMGB = "GB" if print_date_str.count > 4 else "MB"
str = the_date.strftime('%y-%m-%d (%a)')
print("FFF {the_mgb} vs. Me {the_mgb} {date_str}".format(date_str=str, the_mgb=theMGB))
print_date_str.count = 0
if not date_str:
date_str = datetime.datetime.now().strftime('%y-%m')
date_str = date_str + '-' + str(start_day)
the_date = datetime.datetime.strptime(date_str, '%y-%m-%d')
count = 0
while True:
the_delta = datetime.timedelta(days=count)
target_date = the_date + the_delta
if target_date.weekday() in [1, 5] or target_date.day == end_day:
print_date_str(target_date)
if target_date.day == end_day:
break
count += 1