Run Code Python for timestamp format change

Hi Gumloop support,

I’m trying to use code to reformat the timestamp string (from date picker):
2025-04-03T00:00:00

To the format I need:
2025-04-03

I tried Text Formatter Node: Truncate , 10 tokens (as suggested by Gummie, but doesn’t work). I also tried “Find and Replace”, find “T00:00:00”, replaced with “”, also didn’t work. So I resort to Run Code (Python).

def function(check_in_timepicker):
   from datetime import datetime 

   # Convert the input string to the desired format
   check_in_date = datetime.strptime(check_in_timepicker, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d")
    return check_in_date

The error I get is:
" Run Code Failed!
File “/workspace/index.py”, line 6
return check_in_date
IndentationError: unexpected indent"

What did I do wrong?
Thanks so much!!

Wendy

Hey @tamwendy - There’s an indentation issue in the code as the error suggests, can you share a link to your flow so I can take a closer look at the code format?

Also, the Datetime node already has an option to customize the date format, here’s an example: https://www.gumloop.com/pipeline?workbook_id=qPLZzo8CeAj836pCV9gijh&run_id=9dCq93DvQYWVf3TWydqA36

Would this work for you?

https://docs.gumloop.com/nodes/flow_basics/current_datetime

Hi Wasay,

The timestamp is a user input from Interface. Is ‘Datetime’ node for ‘current time’ and not for date format conversion?

Thanks for checking the code:
https://www.gumloop.com/pipeline?workbook_id=eQc6x5tGUDzE8DeKLDwUhG&tab=1

Thank you! The final line in run code has an extra indent, fixed it by adding 4-space indentation.

https://www.gumloop.com/pipeline?workbook_id=ehjrAeaDuU2uhNSvpmyCHP

Hi Wasay,

Would you elaborate where exactly you add the 4 space? (tap space four times?).

I tried to fix it on my workbook and I still get the following error:

Run Code Failed!
File “/workspace/index.py”, line 8
return check_in_date
IndentationError: unexpected indent

Yeah sure - Here’s the before and after of the indentation issue:

Before (with error):

def function(check_in_timepicker):
   from datetime import datetime 
   # Convert the input string to the desired format
   check_in_date = datetime.strptime(check_in_timepicker, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d")
    return check_in_date  # This line has incorrect indentation (extra space before 'return')

After (fixed):

def function(check_in_timepicker):
   from datetime import datetime 
   # Convert the input string to the desired format
   check_in_date = datetime.strptime(check_in_timepicker, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d")
   return check_in_date  # This line now has consistent indentation

Python uses indentation to define code blocks, and all lines within the same block must have the same indentation level. In the original code, the first three lines of the function body use three spaces for indentation, but the return statement uses four spaces.

Hi Wasay,

This is a bit puzzling as I get more error after trying to ensure the same space indent within function call. (also please note the UI have a fixed indent for the return line). Thanks for the guidance!

e

[31m Run Code Failed!
Traceback (most recent call last):
  File "/workspace/index.py", line 8, in <module>
    print('___FUNCTION_OUTPUT___\n' + str(function('')))
  File "/workspace/index.py", line 5, in function
    check_in_date = datetime.strptime(check_in_timepicker, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d")
  File "/usr/local/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/local/lib/python3.10/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S'
e[0m


Hey! This issue is unrelated to indentation, seems like you’re parsing an empty string as datetime. Make sure you’ve all the inputs setup properly in the flow.

check_in_timepicker is an empty string (''), but you’re trying to parse it using the format "%Y-%m-%dT%H:%M:%S".

Hi Wasay,

Yes, you’re right. I probably hit ‘Run’ in the subflow (where I didn’t put in default value) and not the Main flow with the date picker.

But I’m still having a hard time with the ‘indentation error’:

I’ve already made sure there are 4 space of indentation before EACH line.

And I would like to report that when using the date picker interface, the date picked would be one day BEFORE the date selected. Say if I picked April 7th, the date April 6th would be shown. Is this a bug? It seems to be doing some kind of auto UTC time adjustment , but it’s not needed from user’s point of view.

https://www.gumloop.com/interface/Ota-Price-Checker-fSPfgbAYvvxns2KYaRHBhA

Thanks so much

Thank you! Will look into the datetime issue. For the indentation, can you try cloning the workflow I shared and using that instead?

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.