Python. String Date with different formats to single format

Hello,

I am trying to reformat input dates as string to a single format day/month/year, I try with the OOTB node string to date and it seems the input must be month/day/year otherwise it gets null result.

I found this post useful but I was not able to make it work for me https://forum.dynamobim.com/t/python-script-datetime-conversion-list/52898

I got for example this list of dates as strings in sublists:

[[02/23/21],[11/03/2021]],[[03/18/2021]],[[08/10/21],[03/04/2022]],[[31/07/21],[04/05/2021]]

In this list you find 3 formats:

day/month/year YYYY
month/day/year YYYY
day/month/year YY
month/day/year YY

I would like to convert all strings to a list of dates day/month/year YYYY.

What I tried is to split the string with separator β€œ/” and pick the first the second and third parts of it and build a new date, the difficulty to know what date starts with months instead of day, I would only know if the second pair of digits number is higher than 12 months. I think this is simple but looks a nightmare that perhaps there is direct smart solution using python.

I resolved it like this with OOTB nodes and it is a nightmare:

Hi @RubenVivancos

my first thought about this is that if you have everything in one list without having them sorted or some logic how they occured, it is impossible to reformat them. As shown above there is the date β€œ08/10/21” that can be interpreted in two ways day/mounth and mounth/day.

At this point I see one solition that could be helpfull for you. It is to create two dates from one, so you don’t lose any information, but you get needless dates that you can theoretically get rid of.

we could consider by default date is starting by day/month and in rare cases it is with month/day which could be corrected if the second pair of digits is higher than number 12

@RubenVivancos

Do you mean this?

from datetime import datetime #datum en tijd oproepen

now = datetime.now() #datum oproepen (is hierboven geimporteerd)
str_format = "%d-%m-%Y" #formaat van datum bepalen
datum = now.strftime(str_format) #nieuwe lijst maken met datum van vandaag

image

1 Like

should it work like this?

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

dates = IN[0]

reformated_dates = []

for date in dates:
	date = date.split("/")
	if int(date[1]) > 12:
		if len(date[2]) == 2:
			new_date = date[1] + "/" + date[0] + "/20" + date[2]
		else:
			new_date = date[1] + "/" + date[0] + "/" + date[2]
	else:
		if len(date[2]) == 2:
			new_date = date[0] + "/" + date[1] + "/20" + date[2]
		else:
			new_date = date[0] + "/" + date[1] + "/" + date[2]	
	reformated_dates.append(new_date)


OUT = reformated_dates
1 Like

How would python ever know in what format the input date was given?
@RubenVivancos his original post suggest there is a list with dates, in 3 different formats, if the day has a number below 12 its impossible to do if there are three options, right?

The suggestions here work but youd need a manual check for when both date and month number are interchangeable

1 Like

I agree unknown answer here but in case of doubt what you read has format day/month/year as you would do checking visually

exactly what I was asking, but you are supposing the year is starting always with β€œ20”, could be 19,18…

anyway simpliefied with python script much nicer, many thanks.

I updated how I resolved it initially with OOTB nodes on the page so can compare.

if we have only two digits we are not able to identify if date is 1920 or 2020. Here we could put some restrictions let’s say if two digits are bigger than 22 then 19 will be instead of 20. Further distinguishing is not possible without knowing additional details.

1 Like

I improved it like that:

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

dates = IN[0]

reformated_dates = []

for date in dates:
	date = date.split("/")
	if int(date[1]) > 12:
		if len(date[2]) == 2:
			new_date = date[0] + "/" + date[1] + "/20" + date[2]
		else:
			new_date = date[1] + "/" + date[0] + "/" + date[2]
	else:
		if len(date[2]) == 2:
			new_date = date[1] + "/" + date[0] + "/20" + date[2]
		else:
			new_date = date[1] + "/" + date[0] + "/" + date[2]
			
	reformated_dates.append(new_date)

reformated_datestime=[]
for set in reformated_dates:
	sets = DSCore.DateTime.FromString(set)
	reformated_datestime.append(sets)
	
OUT = reformated_datestime