How to Manage Date & Datetime Fields in Odoo 18: Complete Tutorial
By Braincuber Team
Published on April 13, 2026
Odoo 18 provides powerful date and time management capabilities for developers. This complete tutorial covers fields.Date, fields.Datetime, and date_utils tools that make it easier to manipulate, compare, and format date and time values. These utilities are critical for every Odoo app, from scheduling to reporting. Whether you're scheduling tasks, creating reports, or automating workflows, date and time fields in Odoo 18 provide all the tools to handle time-sensitive logic efficiently.
What You'll Learn:
- Understanding fields.Date and fields.Datetime in Odoo 18
- How to use fields.Date.today() for current date
- Working with fields.Datetime.now() for timestamps
- Using date_utils for period calculations
- Adding and subtracting time with date_utils
- Handling timezone-aware timestamps
- Converting between date formats
Getting Started with Date Fields
Odoo 18 provides two main field types for handling date and time data: fields.Date for date-only values and fields.Datetime for date and time combined values. These fields store dates in the database as strings in YYYY-MM-DD format for dates and YYYY-MM-DD HH:MM:SS for datetime values. This beginner guide shows you how to leverage these field types effectively.
fields.Date.today()
The fields.Date.today() method returns the current date in YYYY-MM-DD string format. This is particularly useful for setting default values in models, performing comparisons, and filtering records by today's date.
from odoo import fields
print("Today:", fields.Date.today())
Today: 2023-04-09
fields.Datetime.now()
The fields.Datetime.now() method returns the current datetime in YYYY-MM-DD HH:MM:SS format. This is essential for timestamping events, creating audit logs, and scheduling datetime-based actions in your Odoo modules.
from odoo import fields
print("Now:", fields.Datetime.now())
Now: 2023-04-09 08:12:59
Using date_utils for Period Calculations
Odoo 18's date_utils module provides powerful utilities for manipulating dates and datetime values. These functions are essential for building reports, scheduling, and calculating time periods.
start_of()
Returns the start datetime of a specific period (hour, day, week, month, quarter, year). Use for filtering or range comparison.
end_of()
Returns the ending datetime of a specific period. Essential for report filters and range validations.
add()
Adds relative time (days, weeks, months, years) to a date. Perfect for computing deadlines and expiry dates.
subtract()
Subtracts relative time from a date. Useful for getting historical dates for analytics and reporting.
date_utils.start_of() and end_of()
The start_of() function returns the start datetime of a specific period, while end_of() returns the ending datetime. These are crucial for filtering records and building date ranges in your queries.
from odoo.tools import date_utils
from odoo import fields
today = fields.Datetime.now()
print("Start of Month:", date_utils.start_of(today, "month"))
print("End of Month:", date_utils.end_of(today, "month"))
Start of Month: 2023-04-01 00:00:00
End of Month: 2023-04-30 23:59:59.999999
date_utils.add() and subtract()
These functions allow you to add or subtract relative time from dates. The add() function accepts days, weeks, months, and years as arguments, while subtract() works similarly for historical calculations.
today = fields.Datetime.now()
print("Add 1 Year:", date_utils.add(today, years=1))
print("Subtract 1 Month:", date_utils.subtract(today, months=1))
Practical Applications
Use these functions for computing deadlines, expiry dates, recurring intervals, and analytics that require historical date comparisons.
Other Useful date_utils Functions
Odoo 18 provides several additional date utility functions that simplify common date-related operations in your Odoo modules.
| Function | Description | Example Output |
|---|---|---|
| get_month() | Returns tuple (start, end) of current month | (2023-04-01, 2023-04-30) |
| get_quarter() | Returns tuple (start, end) of current quarter | (2023-04-01, 2023-06-30) |
| get_quarter_number() | Returns quarter number (1-4) | 2 |
| get_fiscal_year() | Returns tuple (start, end) of fiscal year | (2023-01-01, 2023-12-31) |
Timezone Handling in Odoo 18
When working with dates and times across different timezones, Odoo 18 provides two important methods to ensure consistency: context_today() for dates and context_timestamp() for datetime values. These methods adjust the date/time based on the client's timezone setting.
fields.Date.context_today(self)
Returns today's date adjusted to the client's timezone. Essential for ensuring consistency when the user and server are in different time zones.
fields.Datetime.context_timestamp(self, timestamp)
Converts a naive datetime to a timezone-aware one based on user context. Used for correctly storing or comparing timestamps across time zones.
Date Format Conversion
Odoo 18 provides convenient methods to convert between different date formats, which is essential for data processing and display.
# Convert string to date object
value = '2022-01-01'
print("To Date:", fields.Date.to_date(value))
# Convert date object to string
today = fields.Date.today()
print("To String:", fields.Date.to_string(today))
Frequently Asked Questions
What is fields.Date.today() used for in Odoo 18?
It returns the current date in YYYY-MM-DD format. Use it for default values, comparisons, and filtering records by today's date.
How do I add days to a date in Odoo 18?
Use date_utils.add() with the days parameter: date_utils.add(date_value, days=7) to add 7 days to a date.
How does date_utils.start_of() work?
It returns the start datetime of a period. Pass granularity as 'day', 'week', 'month', 'quarter', or 'year' to get the beginning of that period.
How do I handle timezones in Odoo 18?
Use fields.Date.context_today(self) for date and fields.Datetime.context_timestamp(self, timestamp) for datetime values to adjust to the user's timezone.
How do I get the first and last day of the month?
Use date_utils.get_month(date_value) which returns a tuple (start, end) of the month, or use date_utils.start_of() and date_utils.end_of() with granularity='month'.
Need Help with Odoo 18 Development?
Our experts can help you master date handling, build custom modules, and optimize your Odoo implementation. Get free consultation today.
