date_formulas¶
shortfx.fxExcel.date_formulas
¶
Excel-style date functions.
This module provides Excel-compatible date and time functions for Python. All functions follow Excel's naming conventions and behaviors, including Excel's serial number date system (days since December 30, 1899).
Functions¶
DATE(year: int, month: int, day: int) -> float
¶
Returns the Excel serial number corresponding to a particular date. Excel/Spanish name: FECHA
Description: Converts a given year, month, and day into an Excel serial number. The serial number is the number of days since December 30, 1899.
Args: year: The year (1900-9999). month: The month (1-12). day: The day of the month (1-31).
Returns: float: The Excel serial number representing the date.
Raises: ValueError: If the date values are invalid.
Usage Example: >>> DATE(2025, 1, 15) 45667.0 >>> DATE(2024, 12, 31) 45657.0
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
DATEDIF(start_date: datetime, end_date: datetime, unit: str) -> int
¶
Calculates the difference between dates in various units.
Description
Equivalent to Excel's DATEDIF function. Calculates the difference between two dates in complete years, months, days, or partial combinations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date. |
required |
end_date
|
datetime
|
The end date. |
required |
unit
|
str
|
Unit to return:
- |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The difference in the specified unit. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import DATEDIF start = datetime(2020, 1, 15) end = datetime(2025, 3, 20) DATEDIF(start, end, "Y") 5 DATEDIF(start, end, "MD") 5 DATEDIF(start, end, "YM") 2 DATEDIF(start, end, "YD") 64
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
DATEVALUE(date_text: str) -> float
¶
Converts a date in text format to an Excel serial number. Excel/Spanish name: FECHANUMERO
Description: Converts a text representation of a date into an Excel serial number. Accepts dates in DD/MM/YYYY format.
Args: date_text: A date string in DD/MM/YYYY format.
Returns: float: The Excel serial number representing the date.
Raises: ValueError: If the date format is invalid.
Usage Example: >>> DATEVALUE("15/01/2025") 45667.0 >>> DATEVALUE("31/12/2024") 45657.0
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
DAY(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to a day of the month. Excel/Spanish name: DAY (DIA in Spanish contexts)
Description: Extracts the day component (1-31) from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The day of the month (1-31).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> from datetime import datetime >>> DAY(45667.0) # January 15, 2025 15 >>> DAY(datetime(2025, 1, 15)) 15
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
DAYS(end_date: datetime, start_date: datetime) -> int
¶
Returns the number of days between two dates.
Description
Calculates the difference in days between two dates. This is equivalent to Excel's DAYS function. The result can be negative if end_date is before start_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
end_date
|
datetime
|
The end date. |
required |
start_date
|
datetime
|
The start date. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of days between the dates (can be negative). |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import DAYS start = datetime(2025, 1, 1) end = datetime(2025, 1, 15) DAYS(end, start) 14 DAYS(start, end) -14
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
DAYS360(start_date: datetime, end_date: datetime, method: bool = False) -> int
¶
Calculates days between two dates based on a 360-day year.
Description
Equivalent to Excel's DAYS360 function. This function calculates the number of days between two dates using a 360-day year (12 months of 30 days). This is commonly used in financial calculations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date. |
required |
end_date
|
datetime
|
The end date. |
required |
method
|
bool
|
False for US method (NASD), True for European method. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of days based on 360-day year calculation. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import DAYS360 start = datetime(2025, 1, 30) end = datetime(2025, 2, 28) DAYS360(start, end) # US method 28 DAYS360(start, end, method=True) # European method 28
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
EDATE(start_date: Union[datetime, float], months: int) -> float
¶
Returns the serial number of a date months before or after.
Description
Equivalent to Excel's EDATE function. Returns the Excel serial number representing a date that is the indicated number of months before or after the start date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Union[datetime, float]
|
Starting date as datetime or Excel serial number. |
required |
months
|
int
|
Number of months to add (positive) or subtract (negative). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Excel serial number of the resulting date. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import EDATE start = datetime(2025, 1, 31) EDATE(start, 1) # One month later (Feb 28, 2025) 45688.0 EDATE(start, -1) # One month earlier (Dec 31, 2024) 45627.0
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
EOMONTH(start_date: Union[datetime, float], months: int) -> float
¶
Returns the serial number of the last day of the month.
Description
Equivalent to Excel's EOMONTH function. Returns the Excel serial number for the last day of the month that is the indicated number of months before or after start_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Union[datetime, float]
|
Starting date as datetime or Excel serial number. |
required |
months
|
int
|
Number of months to add (positive) or subtract (negative). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Excel serial number of the last day of the resulting month. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import EOMONTH start = datetime(2025, 1, 15) EOMONTH(start, 0) # Last day of January 2025 45658.0 EOMONTH(start, 1) # Last day of February 2025 45686.0
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
HOUR(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to an hour value. Excel/Spanish name: HOUR (HORA as function name in Spanish)
Description: Extracts the hour component (0-23) from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The hour (0-23).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> HOUR(0.5) # 12:00 PM 12 >>> HOUR(0.75) # 6:00 PM 18
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
ISOWEEKNUM(date: datetime) -> int
¶
Returns the ISO week number.
Description
Equivalent to Excel's ISOWEEKNUM function. Returns the ISO week number of the year for a given date (1-53).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
datetime
|
The date to evaluate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
ISO week number (1-53). |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import ISOWEEKNUM ISOWEEKNUM(datetime(2025, 1, 1)) 1 ISOWEEKNUM(datetime(2025, 6, 15)) 24
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
MINUTE(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to a minute value. Excel/Spanish name: MINUTE (MINUTO in Spanish)
Description: Extracts the minute component (0-59) from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The minute (0-59).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> from datetime import datetime >>> MINUTE(datetime(2025, 1, 15, 14, 30)) 30 >>> MINUTE(0.604166667) # Approximately 2:30 PM 30
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
MONTH(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to a month value. Excel/Spanish name: MONTH (MES in Spanish)
Description: Extracts the month component (1-12) from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The month (1-12).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> from datetime import datetime >>> MONTH(45667.0) # January 15, 2025 1 >>> MONTH(datetime(2025, 6, 15)) 6
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
NETWORKDAYS(start_date: datetime, end_date: datetime, holidays: Optional[List[datetime]] = None) -> int
¶
Returns the number of working days between two dates.
Description
Equivalent to Excel's NETWORKDAYS function. Calculates the number of working days (Monday-Friday) between two dates, excluding weekends and optional holidays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date. |
required |
end_date
|
datetime
|
The end date. |
required |
holidays
|
Optional[List[datetime]]
|
List of holiday dates to exclude. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of working days. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import NETWORKDAYS start = datetime(2025, 1, 6) # Monday end = datetime(2025, 1, 10) # Friday NETWORKDAYS(start, end) 5 holidays = [datetime(2025, 1, 8)] NETWORKDAYS(start, end, holidays) 4
Cost: O(n) where n is the number of days between dates
Source code in shortfx/fxExcel/date_formulas.py
NETWORKDAYS_INTL(start_date: datetime, end_date: datetime, weekend: Union[int, str] = 1, holidays: Optional[List[datetime]] = None) -> int
¶
Returns working days between two dates with custom weekend parameters.
Description
Equivalent to Excel's NETWORKDAYS.INTL function. Calculates the number of working days between two dates, allowing customization of which days are considered weekends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date. |
required |
end_date
|
datetime
|
The end date. |
required |
weekend
|
Union[int, str]
|
Weekend definition. Int 1-7 for preset patterns, or string of 0s and 1s where 1=weekend. Defaults to 1. |
1
|
holidays
|
Optional[List[datetime]]
|
List of holiday dates. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of working days. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import NETWORKDAYS_INTL start = datetime(2025, 1, 6) end = datetime(2025, 1, 10) NETWORKDAYS_INTL(start, end) # Standard Sat-Sun weekend 5 NETWORKDAYS_INTL(start, end, weekend=2) # Sun-Mon weekend 4
Cost: O(n) where n is the number of days between dates
Source code in shortfx/fxExcel/date_formulas.py
NOW() -> float
¶
Returns the Excel serial number of the current date and time. Excel/Spanish name: AHORA
Description: Returns the current date and time as an Excel serial number. Integer part is the date, fractional part is the time.
Returns: float: The Excel serial number representing the current date and time.
Raises: None
Usage Example: >>> # Output depends on current date and time >>> NOW() # e.g., 45667.625 for January 15, 2025, 3:00 PM 45667.625
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
SECOND(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to a second value. Excel/Spanish name: SECOND (SEGUNDO in Spanish)
Description: Extracts the second component (0-59) from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The second (0-59).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> from datetime import datetime >>> SECOND(datetime(2025, 1, 15, 14, 30, 45)) 45 >>> SECOND(0.604224537) # Approximately 2:30:05 PM 5
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
TIME(hour: int, minute: int, second: int) -> float
¶
Returns the Excel serial number for a particular time. Excel/Spanish name: HORA (as noun in Spanish)
Description: Converts hours, minutes, and seconds into an Excel serial number representing the time as a fraction of a day.
Args: hour: The hour (0-23). minute: The minute (0-59). second: The second (0-59).
Returns: float: The Excel serial number representing the time (fraction of a day).
Raises: ValueError: If hour, minute, or second are out of range.
Usage Example: >>> TIME(12, 0, 0) # 12:00 PM 0.5 >>> TIME(6, 0, 0) # 6:00 AM 0.25 >>> TIME(18, 30, 0) # 6:30 PM 0.7708333333333334
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
TIMEVALUE(time_text: str) -> float
¶
Converts a time in text format to a serial number.
Description
Equivalent to Excel's TIMEVALUE function. Converts a time represented as text into an Excel serial number (fraction of a 24-hour day).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_text
|
str
|
Time in "HH:MM:SS" format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Excel serial number representing the time. |
Usage Example
from shortfx.fxExcel.date_formulas import TIMEVALUE TIMEVALUE("12:00:00") # Noon 0.5 TIMEVALUE("18:30:00") # 6:30 PM 0.7708333333333334
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
TODAY() -> float
¶
Returns the Excel serial number of the current date. Excel/Spanish name: HOY
Description: Returns the current date as an Excel serial number (integer part only, no time).
Returns: float: The Excel serial number representing today's date.
Raises: None
Usage Example: >>> # Output depends on current date >>> TODAY() # e.g., 45667.0 for January 15, 2025 45667.0
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
WEEKDAY(serial_number: Union[float, datetime], return_type: int = 1) -> int
¶
Converts an Excel serial number to a day of the week. Excel/Spanish name: DIASEM
Description: Returns the day of the week for a given date. The return_type parameter determines how the week is numbered (1-7 or 0-6).
Args: serial_number: An Excel serial number or datetime object. return_type: Determines the week numbering system: - 1 (default): Sunday=1 through Saturday=7 - 2: Monday=1 through Sunday=7 - 3: Monday=0 through Sunday=6
Returns: int: The day of the week according to the return_type.
Raises: ValueError: If return_type is not 1, 2, or 3.
Usage Example: >>> from datetime import datetime >>> # January 15, 2025 is a Wednesday >>> WEEKDAY(datetime(2025, 1, 15)) # Wednesday with Sunday=1 4 >>> WEEKDAY(datetime(2025, 1, 15), 2) # Wednesday with Monday=1 3 >>> WEEKDAY(datetime(2025, 1, 15), 3) # Wednesday with Monday=0 2
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
WEEKNUM(serial_number: Union[float, datetime], return_type: int = 1) -> int
¶
Converts a serial number to a week number.
Description
Equivalent to Excel's WEEKNUM function. Returns the week number for a date. The week number indicates where the week falls numerically within a year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serial_number
|
Union[float, datetime]
|
Date as Excel serial number or datetime. |
required |
return_type
|
int
|
System to use (1=week starts Sunday, 21=ISO week). Defaults to 1. |
1
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Week number of the year. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import WEEKNUM WEEKNUM(datetime(2025, 1, 1), return_type=1) 1 WEEKNUM(datetime(2025, 1, 1), return_type=21) 1
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
WORKDAY(start_date: Union[float, datetime], days: int, holidays: Optional[List[datetime]] = None) -> float
¶
Returns the date after a number of working days.
Description
Equivalent to Excel's WORKDAY function. Returns the Excel serial number of the date before or after a specified number of working days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Union[float, datetime]
|
Starting date. |
required |
days
|
int
|
Number of working days (positive for future, negative for past). |
required |
holidays
|
Optional[List[datetime]]
|
List of holiday dates to exclude. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Excel serial number of the resulting date. |
Usage Example
from datetime import datetime from shortfx.fxDate.date_excel import WORKDAY start = datetime(2025, 1, 6) # Monday WORKDAY(start, 5) # 5 working days later 45662.0 WORKDAY(start, -5) # 5 working days earlier 45652.0
Cost: O(n) where n is the number of days
Source code in shortfx/fxExcel/date_formulas.py
WORKDAY_INTL(start_date: Union[float, datetime], days: int, weekend: Union[int, str] = 1, holidays: Optional[List[datetime]] = None) -> float
¶
Returns the date after working days with custom weekend.
Description
Equivalent to Excel's WORKDAY.INTL function. Returns the Excel serial number of the date before or after a specified number of working days with custom weekend parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
Union[float, datetime]
|
Starting date. |
required |
days
|
int
|
Number of working days to add or subtract. |
required |
weekend
|
Union[int, str]
|
Weekend definition. Defaults to 1 (Sat-Sun). |
1
|
holidays
|
Optional[List[datetime]]
|
List of holiday dates. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Excel serial number of the resulting date. |
Usage Example
from datetime import datetime from shortfx.fxDate.date_excel import WORKDAY_INTL start = datetime(2025, 1, 6) # Monday WORKDAY_INTL(start, 5) # 5 working days later 45662.0 WORKDAY_INTL(start, 5, weekend=2) # With Sun-Mon weekend 45663.0
Cost: O(n) where n is the number of days
Source code in shortfx/fxExcel/date_formulas.py
YEAR(serial_number: Union[float, datetime]) -> int
¶
Converts an Excel serial number to a year value. Excel/Spanish name: YEAR (AÑO in Spanish)
Description: Extracts the year component from an Excel serial number or datetime object.
Args: serial_number: An Excel serial number or datetime object.
Returns: int: The year (e.g., 2025).
Raises: ValueError: If the serial number is invalid.
Usage Example: >>> from datetime import datetime >>> YEAR(45667.0) # January 15, 2025 2025 >>> YEAR(datetime(2024, 12, 31)) 2024
Cost: O(1)
Source code in shortfx/fxExcel/date_formulas.py
YEARFRAC(start_date: datetime, end_date: datetime, basis: int = 0) -> float
¶
Returns the fraction of year between two dates.
Description
Equivalent to Excel's YEARFRAC function. Calculates the fraction of the year represented by the number of whole days between two dates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date. |
required |
end_date
|
datetime
|
The end date. |
required |
basis
|
int
|
Day count basis to use (0=US 30/360, 1=Actual/Actual). Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Fraction of year between the dates. |
Usage Example
from datetime import datetime from shortfx.fxExcel.date_formulas import YEARFRAC start = datetime(2025, 1, 1) end = datetime(2025, 7, 1) YEARFRAC(start, end, basis=0) # US 30/360 0.5 YEARFRAC(start, end, basis=1) # Actual/Actual 0.4958904109589041
Cost: O(1)