Date And Date Time Fields In Odoo
Odoo provides some functions to create date and time objects. These are specific to certain periods like “month”, “quarter”, “fiscal year”, etc. Also in the date_utils, there are a couple of abstractions to make simple date calculations.
- The fields.Date. today() function returns a string with the current date in the format YYYY-MM-DD.
Eg :
print('Today = ', fields.Date.today()
Output :
- The Odoo field.DateTime. now() function returns the current date and time in the format YYYY-MM-DD HH:MM: SS.
Eg:
print('Today = ', fields.Datetime.now())
Output :
- We can import the package date_utils into the module by using
from.odoo.tools import date_utiles
Let’s Discuss some default functions of date utils.
(1) start_of()
Get the start of a time period from a date or a datetime. Start of hour, day, week, month, quarter, and year. start_of(value, granularity) value is the initial date or datetime. Granularity is the type of periods like year, quarter, month, week, day, or hour.
Eg :
print('starting_hour',date_utils.start_of(today, "hour")) print('Today = ', fields.Datetime.now()) print('starting_day',date_utils.start_of(today, "day")) print('starting_week',date_utils.start_of(today, "week")) print('starting_month',date_utils.start_of(today, "month")) print('starting_quarter',date_utils.start_of(today, "quarter")) print('starting_year',date_utils.start_of(today, "year"))
Output:
(2) end_of()
In Odoo datetime field, Using the end_of function we can get the ending of hour, day, week, month, quarter, and year of date or datetime. end_of(value, granularity) value is the initial date or datetime. And granularity is the Type of periods like year, quarter, month, week, day, or hour.
Eg:
print('Today = ', fields.Datetime.now()) print('ending of hour =',date_utils.end_of(today, "hour")) print('ending of day =', date_utils.end_of(today, "day")) print('ending of week =', date_utils.end_of(today, "week")) print('ending of month =', date_utils.end_of(today, "month")) print('ending of quarter =', date_utils.end_of(today, "quarter")) print('ending of year =', date_utils.end_of(today, "year"))
Output :
(3) add()
Return the sum of value and a relative delta. We just need to mention the correct number of days.
- date_utils.add(today,days=1) function add one days with today
- date_utils.add(today,weeks=1) function add one weeks with today
- date_utils.add(today,months=1) function add one month with today
- date_utils.add(today,years=1) function add one year with today
- date_utils.add(today, days=2, months=6, years=1) we can also add days, months, and years together.
Eg
print('Today = ', fields.Datetime.now()) print('add days =',date_utils.add(today, days=1)) print('add week =',date_utils.add(today, weeks=1)) print('add month =',date_utils.add(today, months=1)) print('add year =',date_utils.add(today, years=1)) print('add day and year and month =',date_utils.add(today, days=1, months=1, years=1))
Output :
(4) Subtract ()
Return the difference between value and a relative delta. Passing the parameter number of days to be subtracted from the current date and time.
- date_utils.subtract(today, days=1) function one day subtracted from the current date and time.
- date_utils.subtract(today, weeks=1) function one week subtracted from the current date and time.
- date_utils.subtract(today, months=1) function one month subtracted from the current date and time.
- date_utils.subtract(today, years=1) function one year subtracted from the current date and time.
- date_utils.subtract(today, days=2, months=6, years=1) we can subtract days, months, and years from the current day and time together.
Eg
print('Today = ', fields.Datetime.now()) print('subtract day =',date_utils.subtract(today, days=1)) print('subtract week =',date_utils.subtract(today, weeks=1)) print('subtract month =', date_utils.subtract(today, months=1)) print('subtract year =', date_utils.subtract(today, years=1)) print('subtract day and month =', date_utils.subtract(today, days=2, months=6))
Output :
(5) get_month()
get_month = date_utils.get_month(today) It returns the range from the first day of the month to the last.
Eg:
print('Get month =',date_utils.get_month(today))
Output :
(6)get_quarter()
get_quarter = date_utils.get_quarter(today) It returns the range from the first day of the fiscal quarter to the last.
Eg :
print(‘Get Quarter =’, date_utils.get_quarter(today))
Output :
(7)get_quarter_number()
quarter_number = date_utils.get_quarter_number(today) It returns the number of the current quarter.
Eg :
print('Get Quarter Number =', date_utils.get_quarter_number(today))
Output :
(8) get_fiscal_year()
fiscal_year = date_utils.get_fiscal_year(today) It returns the range from the first day of the fiscal year to the last.
Eg :
print('Get Fiscal Year =', date_utils.get_fiscal_year(today))
Output :
(9)context_today()
today = fields.Date.context_today() Return the current date as seen in the client’s timezone in a format fit for date fields.
Eg :
print('context', fields.Date.context_today(self))
Output :
(10) context_timestamp()
It returns the given timestamp converted to the client’s timezone.
Eg :
print('Context timestamp =', Today = 2023-04-09 10:14:00 Context timestamp = 2023-04-09 15:44:00+05:30
Output :
(11) to_date()
fields.Date.to_date(value) It converts the value to a date object.
Eg :
value = '2022-01-01' print('Convert to date =',fields.Date.to_date(value)) print('Data type =',type(fields.Date.to_date(value)))
Output :
(12) to_string()
fields.Date.to_string(value) Convert the date to a string object.
Eg :
print('Today = ', fields.Datetime.now()) print('Convert to string =', fields.Date.to_string(today)) print('Data Type =',type(fields.Date.to_string(today)))
Output :
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote