-1

Here i am getting a list of dates check whether the day is sunday or not

date_list = ['2023-03-17', '2023-03-18', '2023-03-19', '2023-03-20']

if date is sunday i need to create my table record where capacity is 0 and if not sunday i need to create record with capacity as 10

for date in date_list:
    if date.weekday() > 5:
       people = '5'
       capacity = 0
       ProductionCapacity.objects.create(num_of_people=people,date=date,capacity=capacity)
    else:
       people = '15'
       capacity = 10
       ProductionCapacity.objects.create(num_of_people=people,date=date,capacity=capacity)
Harika
  • 1
  • 2

2 Answers2

0

You can loop through every item of the list and check what date it is on using the datetime module. You must first convert each item to a date using datetime.datetime.strptime.

import datetime # import the datetime module, it is defaultly installed

date_list = ['2023-03-17', '2023-03-18', '2023-03-19', '2023-03-20']
sundays = [] # create a list to store the sundays

for date in date_list: # loop through every date in the list
    if datetime.datetime.strptime(date, '%Y-%m-%d').weekday() == 6: # it will return 0 for monday 6 for sunday and so on
        sundays.append(date) # if it is a sunday then add it to the list of sundays

print(sundays)

The list "sundays" will contain all the dates that are on a Sunday.

0
import pandas as pd
date_list = ['2023-03-17', '2023-03-18', '2023-03-19', '2023-03-20']
for i in date_list:
    d = pd.Timestamp(i)
    if (d.day_name()=="Sunday"):
         print(i,"is Sunday")   

The output will be 2023-03-19 is Sunday