Nodes/ComfyUI-Data-Analysis/Py Datetime To String
ComfyUI Node

Py Datetime To String

A Date to a Filename-Ready String, Formatted Your Way

By HowToSD·Created 2 years ago·Updated about a year ago· 23
Py Datetime To String
  • value
  • STRING
date_format%Y-%m-%d %H:%M:%S

Somewhere in your workflow you'll have a datetime and a place that only accepts text - a filename, a label, a text node. Py Datetime To String is the converter for that gap: it takes a PYDATETIME and a format string and hands you back a STRING.

It's one of those boring nodes you won't think about until the moment you're staring at datetime.datetime(2025, 3, 10, 14, 30, 0) in a widget and wishing it said "2025-03-10". That's the whole job, and it does it cleanly.

How it works

It's datetime.strftime(value, date_format) - the Python strftime method, which means the date_format field uses Python's format codes, not anything ComfyUI-specific. The default is %Y-%m-%d %H:%M:%S, which renders a timestamp like 2025-03-10 14:30:00. Swap in %Y%m%d for a filename-friendly 20250310, or %B %Y for "March 2025". The full code table lives in the Python datetime docs.

Inputs and outputs

  • value - the PYDATETIME you want formatted.
  • date_format - the strftime format string. Default %Y-%m-%d %H:%M:%S.

Output is a single STRING, which you can wire into text display nodes, filename builders, or any node that eats strings.

Install

From the HowToSD/ComfyUI-Data-Analysis pack. Manager: search "Data analysis" in the Custom Node Manager, install ComfyUI-Data-Analysis, restart, reload the browser. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Data-Analysis.git
mv ComfyUI-Data-Analysis data-analysis   # README: example workflows need this name
pip install -r requirements.txt

Lightweight node, no models, no GPU needed - it's a datetime cast, not a neural net.

Common issues

The only real trap: format codes must match what you intend, and the output isn't validated against anything. %M is minutes and %m is month - swap them and you'll get quietly wrong strings. Round-tripping through Py String To Datetime requires the format to match the original string exactly, so if you reformat one way and parse the other, keep the two format strings in sync. Also note this formats an existing datetime - if your date is sitting in a text field, go the other direction with Py String To Datetime first.

CategoryData Analysis

Inputs (2)

NameTypeDefaultDescription
valuePYDATETIME
date_formatSTRING%Y-%m-%d %H:%M:%S

Outputs (1)

NameTypeDescription
STRINGSTRING