Date Helper
Class for simplyify date operations.
This class provides static methods to handle date formatting, parsing, and calculations. It defaults to the "YYYY-MM-DD" format for date strings, but this can be overridden by passing a different format string. It also handles timezone-aware dates by using the local timezone of the system when parsing and formatting dates.
add_days(start_date, days)
staticmethod
Add days to a date object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_date
|
date
|
The date to which days will be added. |
required |
days
|
int
|
The number of days to add. |
required |
Returns:
Name | Type | Description |
---|---|---|
date |
date
|
A new date object with the added days, or None if start_date or days is None. |
days_between(start_date, end_date)
staticmethod
Calculate the number of days between two date objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_date
|
date
|
The start date. |
required |
end_date
|
date
|
The end date. |
required |
Returns:
Name | Type | Description |
---|---|---|
difference |
int
|
The number of days between the two dates, or None if either date is None. |
format_date(date_obj, date_format='%Y-%m-%d')
staticmethod
Format a date object to a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_obj
|
date
|
The date object to format. |
required |
date_format
|
Optional[str]
|
The format string to use for formatting the date. |
'%Y-%m-%d'
|
Returns:
Name | Type | Description |
---|---|---|
date_str |
str
|
The formatted date string, or None if date_obj is None. |
get_file_date(file_path)
staticmethod
Get the last modified date of a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str | Path
|
Path to the file. Cane be a string or a Path object. |
required |
Returns:
Name | Type | Description |
---|---|---|
date_obj |
date
|
The last modified date of the file as a date object, or None if the file does not exist. |
is_valid_date(date_str, date_format='%Y-%m-%d')
staticmethod
Check if a date string is valid according to the specified format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_str
|
str
|
The date string to check. |
required |
date_format
|
Optional[str]
|
The format string to use for checking the date. Defaults to "%Y-%m-%d". |
'%Y-%m-%d'
|
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if the date string is valid, False otherwise. |
parse_date(date_str, date_format='%Y-%m-%d')
staticmethod
Parse a date string to a date object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_str
|
str
|
The date string to parse. |
required |
date_format
|
Optional[str]
|
The format string to use for parsing the date. Defaults to "%Y-%m-%d". |
'%Y-%m-%d'
|
Returns:
Name | Type | Description |
---|---|---|
date_obj |
date
|
A date object representing the parsed date, or None if date_str is empty. |
today()
staticmethod
Get today's date.
Returns:
Name | Type | Description |
---|---|---|
result |
date
|
Today's date as a date object, using the local timezone. |
today_add_days(days)
staticmethod
Get today's date ofset by days.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
days
|
int
|
The number of days to offset from today. Can be positive or negative |
required |
Returns:
Name | Type | Description |
---|---|---|
result |
date
|
Today's date offset by the specified number of days. |
today_str(date_format='%Y-%m-%d')
staticmethod
Get today's date in string format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
date_format
|
Optional[str]
|
The format string to use for formatting the date. |
'%Y-%m-%d'
|
Returns:
Name | Type | Description |
---|---|---|
result |
str
|
Today's date as a formatted string, using the specified date format. |