-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) 
2
  • Show us your code, even if it didn't work. What have you tried? Commented Mar 6, 2023 at 7:18
  • 2
    Why do you say "in Django"? Is this data in a form? model? something else? There are more effective ways to do this. Commented Mar 6, 2023 at 7:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.