Here are several examples of how to format dates and times using the Luxon library's toFormat() function within n8n's Expression node. These examples assume you want to generate a timestamped folder name.
Note: These examples use DateTime.now() to get the current date and time. Remember that this uses the n8n server's timezone by default. You can specify a timezone using .setZone('Your/Timezone').
Examples:
-
ddmmyy_hhmm (Date and Time - as requested)
{{ DateTime.now().toFormat('ddMMyy_HHmm') }} // Example Output: 010425_1435 (April 1, 2025, 2:35 PM)
-
ddmmyy (Date Only)
{{ DateTime.now().toFormat('ddMMyy') }} // Example Output: 010425 (April 1, 2025)
-
yyyymmdd (Date Only - ISO style)
{{ DateTime.now().toFormat('yyyyMMdd') }} // Example Output: 20250401 (April 1, 2025)
-
mmddyyyy (Date Only - US style)
{{ DateTime.now().toFormat('MMddyyyy') }} // Example Output: 04012025 (April 1, 2025)
-
yyyy-MM-dd (Date Only - ISO with Hyphens)
{{ DateTime.now().toFormat('yyyy-MM-dd') }} // Example Output: 2025-04-01 (April 1, 2025)
-
hhmmss (Time Only)
{{ DateTime.now().toFormat('HHmmss') }} // Example Output: 143542 (2:35:42 PM)
-
HH:mm:ss (Time with colons)
{{ DateTime.now().toFormat('HH:mm:ss') }} //Example Output: 14:35:42
-
yyyyMMdd_HHmmss (Date and Time - All numbers)
{{ DateTime.now().toFormat('yyyyMMdd_HHmmss') }} // Example Output: 20250401_143542 (April 1, 2025, 2:35:42 PM)
-
MMMM dd, yyyy (Long Date Format)
{{ DateTime.now().toFormat('MMMM dd, yyyy') }} // Example Output: April 01, 2025
-
EEE, MMM d, ''yy (Short Date with Day)
{{ DateTime.now().toFormat("EEE, MMM d, ''yy") }}
// Example Output: Tue, Apr 1, '25
```
11. **yyMMddHHmmssSSS (Including milliseconds)**
```javascript
{{ DateTime.now().toFormat('yyMMddHHmmssSSS') }}
// Example Output: 250401143542123 (April 1, 2025, 2:35:42 PM and 123 milliseconds)
```
12. **yyMMdd_HH-mm-ss (Date and Time with mixed separators)**
```javascript
{{ DateTime.now().toFormat('yyMMdd_HH-mm-ss') }}
// Example output: 250401_13-44-22