How to Get the Position of Input Date in the Year with Python


In this example we will show how to determine the position of the input date in the year with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

syl = str(input("please input your date like xxxx/xx/xx: "))
a = (31,28,31,30,31,30,31,31,30,31,30,31)
b = syl.split('/')
sum = 0
days = 0
for i in range(0, int(b[1]) - 1):
    sum = sum + int(a[i])
days = sum + int(b[2])
if int(b[0]) % 100 != 0 and int(b[0]) % 4 == 0 or int(b[0]) % 400 == 0 and int(b[1])>=3:
    print(days+1)
else:
    print(days)

Output:

please input your date like xxxx/xx/xx: 2017/03/01
60

This indicates that the day of 2017/03/01 is the 60th day of this year.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments