Python script - datetime conversion list

Hi there,
I’m new to Dynamo and to Python scripts as well.
I’m creating a Dynamo routine to create sheets in Revit from data in an Excel file.
I wish to set some sheets parameters as well (Drawn By, Designed By, etc).
Each Excel column contains all data for one variable (i.e. Drawn By), so I get several Dynamo lists to be fed into Revit.
When it comes to Sheet Issue Date I get dates formatted like “11/07/2020 00.00.00” and I want to reformat them to something like “11-07-2020”.
I tried to create a Dynamo script with datetime.strptime(*originalData*, "%d/%m/%Y").strftime(str_format).
It performs good if I feed in to Python Script a single string, but I’m struggling to do the same with all the values from within a list.
Following the Python code and a screenshot:

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from datetime import datetime

# The inputs to this node will be stored as a list in the IN variables.

# Place your code below this line
str_format = IN[0]
myDatesList = IN[1]

myNewDatesList = []

# Assign your output to the OUT variable.

for d in myDatesList:
	myNewDatesList.append(datetime.strptime(d, "%d/%m/%Y").strftime(str_format))

OUT = myNewDatesList

Thank you in advance for your support.
Best regards,
PP

What does the yellow error say for the python node?

Python_ReformatListOfDates_Error

Row 27: it expects a string but I passed a datetime.
Don’t know how to fix it.
Should I convert the value to string?

Thank you

Yeah the inputted date seems to want to be a string not a “DateTime” element. Therefore add a “.ToString()” to end of the “d”.

1 Like

Thank you Brendan,
I tried your suggestion and the type mismatch was solved.
However I still get an error for the format now

ValueError: time data does not match format
data=**12/07/2020 00.00.00**, fmt=%d/%m/%Y, to: d'/'M'/'yyyy

The date between asterisks is in the format I get from my Excel file (see first pic I uploaded, in the list of retrieved data below the “SheetIssueDate List.GetItemAtIndex” node).

Thanks again.

Your string format of %d%m%Y does not match the inputted date and time format “12/07/2020 00.00.00”

You should have it as “%d%m%Y %H:%M:%S”

Therefore :
datetime.strptime(d, “%d%m%Y”)

Becomes:
datetime.strptime(d.ToString(), “%d%m%Y %H:%M:%S”)

Thanks again Brendan!
By the time I wrote my previous post I figured it out myself by reading the https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

I changed the code as following and now it works like a charm
myNewDatesList.append(datetime.strptime(d.ToString(), "%d/%m/%Y %H.%M.%S").strftime(str_format))

Thanks again for your support.
Best regards.

1 Like