Skip to content

Instantly share code, notes, and snippets.

@qzcool
Last active June 23, 2019 06:40
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 qzcool/c46bc99d3477975a76fdbf700687c366 to your computer and use it in GitHub Desktop.
Save qzcool/c46bc99d3477975a76fdbf700687c366 to your computer and use it in GitHub Desktop.
Get the calendar position of start and end date of any date range
import datetime
date_start = datetime.datetime.strptime('2019-09-26', '%Y-%m-%d').date()
date_end = date_start + datetime.timedelta(days=1)
# 每月第一天的星期日名称,并推算date_start日和T+1日的表格位置
def get_calender_position_x(date):
position_x = date.weekday() + 1
if position_x == 7:
position_x = 0
position_x += 1
return position_x
def get_calender_position_y(date):
position_y = 1
lines_increment = (date.day-1)//7
lines_increment_residual = (date.day-1)%7
position_x = get_calender_position_x(date=date.replace(day=1))
if position_x + lines_increment_residual > 7:
lines_increment += 1
position_y += lines_increment
return position_y
position_date_start_x = get_calender_position_x(date=date_start)
position_date_end_x = get_calender_position_x(date=date_end)
position_date_start_y = get_calender_position_y(date=date_start)
position_date_end_y = get_calender_position_y(date=date_end)
print ('Start:', position_date_start_y, position_date_start_x)
print ('End:', position_date_end_y, position_date_end_x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment