Enable Dark Mode!
how-to-manage-date-datetime-fields-in-odoo-18.jpg
By: Ayana KP

How to Manage Date & Datetime Fields in Odoo 18

Technical Odoo 18

Odoo 18 provides powerful date and time management capabilities to developers through its robust fields.Date, fields.Datetime, and date_utils tools. These utilities make it easier to manipulate, compare, and format date and time values, which are critical in every Odoo app, from scheduling to reporting.

In this blog, we’ll explore how to work with date and datetime fields and Odoo’s date utilities.

1. fields.Date.today()

Returns the current date in YYYY-MM-DD string format.

Purpose: Useful for default values in models, comparisons, and filtering records by today's date.

from odoo import fields
print("Today:", fields.Date.today())
Output:
Today: 2023-04-09

2. fields.Datetime.now()

Returns the current datetime in YYYY-MM-DD HH:MM:SS format.

Purpose: Used to timestamp events, create audit logs, or schedule datetime-based actions.

from odoo import fields
print("Now:", fields.Datetime.now())

Output:

Now: 2023-04-09 08:12:59

3. date_utils.start_of(value, granularity)

Returns the start datetime of a specific period.

Granularity Options: hour, day, week, month, quarter, year

Purpose: Normalize a datetime to the beginning of a time unit for filtering or range comparison.

from odoo.tools import date_utils
from odoo import fields
today = fields.Datetime.now()
print("Start of Month:", date_utils.start_of(today, "month"))
Output:
Start of Month: 2023-04-01 00:00:00

4. date_utils.end_of(value, granularity)

Returns the ending datetime of a specific period.

Purpose: Get the latest timestamp of a unit like day, week, month, etc., for report filters or range validations.

print("End of Month:", date_utils.end_of(today, "month"))

Output:

End of Month: 2023-04-30 23:59:59.999999

5. date_utils.add(value, **kwargs)

Adds a relative time to the date/datetime.

Accepted Args: days, weeks, months, years

Purpose: Used in computing deadlines, expiry dates, recurring intervals.

print("Add 1 Year:", date_utils.add(today, years=1))

Output:

Add 1 Year: 2024-04-09 09:20:13

6. date_utils.subtract(value, **kwargs)

Subtracts a relative delta from a date or datetime.

Purpose: Get historical dates like previous month, last year for analytics.

print("Subtract 1 Month:", date_utils.subtract(today, months=1))

Output:

Subtract 1 Month: 2023-03-09 09:32:04

7. date_utils.get_month(value)

Returns a tuple (start, end) of the current month.

Purpose: Used in monthly reports and summaries.

print("Current Month Range:", date_utils.get_month(today))

Output:

(datetime.datetime(2023, 4, 1, 0, 0), datetime.datetime(2023, 4, 30, 0, 0))

8. date_utils.get_quarter(value)

Returns a tuple (start, end) of the current quarter.

Purpose: Generate quarterly reports, track performance per fiscal quarter.

print("Current Quarter:", date_utils.get_quarter(today))

Output:

(datetime.datetime(2023, 4, 1, 0, 0), datetime.datetime(2023, 6, 30, 0, 0))

9. date_utils.get_quarter_number(value)

Returns the quarter number (1–4) of the given date.

Purpose: Useful in logic that depends on which quarter the date falls in.

print("Quarter Number:", date_utils.get_quarter_number(today))

Output:

Quarter Number: 2

10. date_utils.get_fiscal_year(value)

Returns a tuple (start, end) for the fiscal year of the given date.

Purpose: Crucial for fiscal reporting, compliance, accounting periods.

print("Fiscal Year:", date_utils.get_fiscal_year(today))

Output:

(datetime.datetime(2023, 1, 1, 0, 0), datetime.datetime(2023, 12, 31, 0, 0))

11. fields.Date.context_today(self)

Returns today’s date adjusted to the client’s timezone.

Purpose: Ensures consistency when the user and server are in different time zones.

print("Context Today:", fields.Date.context_today(self))

12. fields.Datetime.context_timestamp(self, timestamp)

Converts a naive datetime to a timezone-aware one based on user context.

Purpose: For correctly storing or comparing timestamps across time zones.

print("Context Timestamp:", fields.Datetime.context_timestamp(self, today))

13. fields.Date.to_date(value)

Converts a date string (YYYY-MM-DD) into a datetime.date object.

Purpose: Needed when performing operations on Python date objects.

value = '2022-01-01'
print("To Date:", fields.Date.to_date(value))

14. fields.Date.to_string(value)

Converts a Python date object to a string format YYYY-MM-DD.

Purpose: For displaying or serializing dates.

print("To String:", fields.Date.to_string(today))

Whether you're scheduling tasks, creating reports, or automating workflows, date and time fields in Odoo 18 provide you with all the tools to handle time-sensitive logic efficiently. Mastering these utilities will boost your productivity and make your modules more powerful and reliable.

To read more about How to Create Calendar View in Odoo 17 ERP, refer to our blog How to Create Calendar View in Odoo 17 ERP.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message