datetime
datetime is Python's built-in datetime.datetime class, exposed inside the formula sandbox as a constructor. Calling it returns a datetime object representing a specific date and time. Use datetime() for timestamp comparisons, date arithmetic with time precision, and when constructing datetime values for filter conditions.
Syntax
datetime(year, month, day, hour=0, minute=0, second=0)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| year | int | Yes | The year (e.g., 2024) |
| month | int | Yes | The month (1–12) |
| day | int | Yes | The day of the month (1–31) |
| hour | int | No | The hour (0–23). Default: 0 |
| minute | int | No | The minute (0–59). Default: 0 |
| second | int | No | The second (0–59). Default: 0 |
Returns
Type: datetime
A datetime object representing the specified date and time.
Class Methods
| Method | Description | Example |
|---|---|---|
datetime.now() |
Returns the current date and time | datetime.now() |
datetime.strptime(string, fmt) |
Parse a string into a datetime | datetime.strptime('2024-06-15', '%Y-%m-%d') |
Instance Methods and Properties
| Method/Property | Description | Example |
|---|---|---|
.year |
The year | datetime.now().year |
.month |
The month | datetime.now().month |
.day |
The day | datetime.now().day |
.hour |
The hour | datetime.now().hour |
.minute |
The minute | datetime.now().minute |
.second |
The second | datetime.now().second |
.date() |
Extract the date portion | datetime.now().date() |
.time() |
Extract the time portion | datetime.now().time() |
.isoformat() |
ISO 8601 string | datetime.now().isoformat() |
.strftime(fmt) |
Format as string | datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
.replace(...) |
Return datetime with replaced fields | datetime.now().replace(hour=0, minute=0, second=0) |
Common Format Codes
| Code | Meaning | Example Output |
|---|---|---|
%Y |
4-digit year | 2024 |
%m |
2-digit month | 06 |
%d |
2-digit day | 15 |
%H |
24-hour hour | 14 |
%M |
Minute | 30 |
%S |
Second | 00 |
%B |
Full month name | June |
%b |
Abbreviated month name | Jun |
%A |
Full weekday name | Saturday |
Examples
Get the current date and time:
datetime.now()
Create a specific datetime:
datetime(2024, 6, 15, 14, 30, 0)
Calculate 30 days ago:
datetime.now() - timedelta(days=30)
Start of today (midnight):
datetime.now().replace(hour=0, minute=0, second=0)
Format for display:
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Produces '2024-06-15 14:30:00'.
Parse a date string:
datetime.strptime([created_on], '%Y-%m-%d %H:%M:%S')
Converts a string field into a datetime object for arithmetic or comparison.
Days since a record was created:
(datetime.now() - datetime.strptime([created_on], '%Y-%m-%d %H:%M:%S')).days
Using datetime() in Filter Conditions
When building standard filter conditions that compare against datetime fields, use the datetime() function to construct values:
datetime.now() - timedelta(days=90)
This ensures the value is a proper datetime object that PB translates into the source system's format. Always use date() or datetime() constructors for date comparisons in filters — do not pass raw date strings.
Notes
- Datetime arithmetic uses
timedelta— see timedelta. - For date-only values (no time component), use date.
- Datetimes support comparison operators:
<,>,<=,>=,==,!=. - Subtracting two datetimes produces a
timedeltaobject.
Related Documentation
- date — Date-only values
- timedelta — Duration for date arithmetic
- Working with Types — Overview of all data types
- Formulas in Filters — Using datetime() in filter conditions
Comments
0 comments
Please sign in to leave a comment.