Skip to main content
Question

Failed to convert string to date in python code component

  • October 16, 2024
  • 1 reply
  • 11 views

  • Beginner
  • 6 replies

My python code runs normally locally, but it fails when I put it in code component. I am now using a for loop to convert one of the date items from a string to a date, and then compare the size with the current date, but now it fails. The code component prompts me that the parameter passed in [datetime.datetime.strptime(dueDateTimeStr, "%Y-%m-%d")] is None

 

Can you help me see why it fails? Or code component is not applicable to string conversion date

Full Code


import json
import datetime

# Get the input JSON string
res_body = input_data.get('value', '{}')
res_body_json = json.loads(res_body)
rst=[]

#获取当天日期
now_date_str = datetime.date.today().strftime('%Y-%m-%d')
now_date = datetime.datetime.strptime(now_date_str, "%Y-%m-%d")

for entry in res_body_json['value']:
    # 获取当前任务的截至日期
    dueDateTimeStr = str(entry.get("dueDateTime",{}).get("dateTime"))[:10]
    dueDateTime1 = datetime.datetime.strptime(dueDateTimeStr, "%Y-%m-%d")
    rst_row={
          "title": entry["title"],
          "dueDateTime": dueDateTimeStr,  
          "checklistItems": [{"item_name": item["displayName"]}  for item in entry.get("checklistItems", []) if item["isChecked"] is False]  
      }
    rst.append(rst_row)


output =  {"rst": rst}

 

Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

  • Author
  • Beginner
  • 6 replies
  • October 16, 2024

I have solved this problem. It is caused by incomplete test data in my local area. Thank you very much for your help😀