function format_datetime
Formats a datetime object into a standardized string representation for display purposes, returning an empty string if the input is None.
/tf/active/vicechatdev/CDocs/utils/__init__.py
35 - 47
simple
Purpose
This utility function provides consistent datetime formatting across the CDocs application. It handles None values gracefully by returning empty strings, making it safe to use in display contexts where datetime values may be optional. The function relies on a DATETIME_FORMAT constant (not shown in the code) that defines the application's standard datetime format.
Source Code
def format_datetime(date_obj: Optional[datetime]) -> str:
"""
Format datetime for display.
Args:
date_obj: Datetime to format
Returns:
Formatted datetime string or empty string if None
"""
if date_obj is None:
return ""
return date_obj.strftime(DATETIME_FORMAT)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
date_obj |
Optional[datetime] | - | positional_or_keyword |
Parameter Details
date_obj: An optional datetime object to be formatted. Can be None, in which case the function returns an empty string. When provided, should be a valid Python datetime object from the datetime module.
Return Value
Type: str
Returns a string representation of the datetime formatted according to the DATETIME_FORMAT constant. If the input date_obj is None, returns an empty string (''). The exact format of the returned string depends on the DATETIME_FORMAT constant defined elsewhere in the codebase.
Dependencies
datetime
Required Imports
from datetime import datetime
from typing import Optional
Usage Example
from datetime import datetime
from typing import Optional
# Define the required constant
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
def format_datetime(date_obj: Optional[datetime]) -> str:
if date_obj is None:
return ""
return date_obj.strftime(DATETIME_FORMAT)
# Example usage
now = datetime.now()
formatted = format_datetime(now)
print(f"Formatted datetime: {formatted}")
# Output: Formatted datetime: 2024-01-15 14:30:45
# Handle None case
formatted_none = format_datetime(None)
print(f"Formatted None: '{formatted_none}'")
# Output: Formatted None: ''
# Use in display context
user_last_login = None
print(f"Last login: {format_datetime(user_last_login) or 'Never'}")
# Output: Last login: Never
Best Practices
- Ensure DATETIME_FORMAT constant is defined before calling this function
- Use this function consistently throughout the application for uniform datetime display
- The function is null-safe, making it ideal for optional datetime fields in data models
- Consider the timezone implications - the function formats whatever timezone is in the datetime object without conversion
- For user-facing displays, ensure DATETIME_FORMAT is set to a user-friendly format appropriate for your locale
- When using in templates or UI contexts, the empty string return for None values allows for clean display without additional null checks
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function format_date 95.1% similar
-
function parse_date 63.3% similar
-
function format_datetime_v1 62.2% similar
-
function parse_datetime 59.1% similar
-
function parse_datetime_v1 48.5% similar