64 def datetime_to_string(self, datetime_instance: datetime, date_only: bool =
False, sql_mode: bool =
False) -> str:
65 """Convert a datetime object to a formatted string.
68 datetime_instance (datetime): Datetime to format.
69 date_only (bool, optional): Return only the date portion. Defaults to False.
70 sql_mode (bool, optional): Include millisecond precision for SQL. Defaults to False.
73 ValueError: If the input is not a datetime instance.
76 str: Formatted datetime string.
78 if not isinstance(datetime_instance, datetime):
80 "The input is not a datetime instance.",
83 raise ValueError(
"Error: Expected a datetime instance.")
85 return datetime_instance.strftime(self.
date_only)
86 converted_time = datetime_instance.strftime(self.
date_and_time)
88 microsecond = datetime_instance.strftime(
"%f")[:3]
89 return f
"{converted_time}.{microsecond}"
92 def string_to_datetime(self, datetime_string_instance: str, date_only: bool =
False) -> datetime:
93 """Convert a formatted string to a datetime object.
96 datetime_string_instance (str): Formatted datetime string.
97 date_only (bool, optional): Parse using the date-only format. Defaults to False.
100 ValueError: If the input is not a string instance.
103 datetime: Parsed datetime object.
105 if not isinstance(datetime_string_instance, str):
107 "The input is not a string instance.",
110 raise ValueError(
"Error: Expected a string instance.")
112 return datetime.strptime(datetime_string_instance, self.
date_only)
113 return datetime.strptime(datetime_string_instance, self.
date_and_time)