Blog

odoo-datetime-field
Uncategorized

Date And Date Time Fields In Odoo

RINSHA
September 13, 2023

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 :   

Today =               2023-04-09

  • 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 :

Today =  2023-04-09 08:12:59

  • 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:

Today =  2023-04-09 09:54:05
starting_hour 2023-04-09 09:00:00
starting_day 2023-04-09 00:00:00
starting_week 2023-04-03 00:00:00
starting_month 2023-04-01 00:00:00
starting_quarter 2023-04-01 00:00:00
starting_year 2023-01-01 00:00:00

(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 :

Today =  2023-04-09 09:10:37
ending of hour = 2023-04-09 09:59:59.999999

Request Your Free Quote

(3)   add()

Return the sum of value and a relativedelta. 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 :

Today =  2023-04-09 09:20:13
add days = 2023-04-10 09:20:13
add week = 2023-04-16 09:20:13
add month = 2023-05-09 09:20:13
add year = 2024-04-09 09:20:13
add day and year and month = 2024-05-10 09:20:13

(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 :

Today =  2023-04-09 09:32:04
subtract day = 2023-04-08 09:32:04
subtract week = 2023-04-02 09:32:04
subtract month = 2023-03-09 09:32:04
subtract year = 2022-04-09 09:32:04
subtract day and month = 2022-10-07 09:32:04

(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 :

Today =  2023-04-09 09:39:25
Get month = (datetime.datetime(2023, 4, 1, 0, 0),
datetime.datetime(2023, 4, 30, 0, 0))

(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 :

Today =  2023-04-09 09:48:29
Get Quarter = (datetime.datetime(2023, 4, 1, 0, 0),
datetime.datetime(2023, 6, 30, 0, 0))

(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 :

Today =  2023-04-09 09:51:54
Get Quarter Number = 2

(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 :

Today =  2023-04-09 09:54:05
Get Fiscal Year = (datetime.datetime(2023, 1, 1, 0, 0),
datetime.datetime(2023, 12, 31, 0, 0))

(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 :

context 2023-04-09

(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 :

Today =  2023-04-09 10:14:00
Context timestamp = 2023-04-09 15:44:00+05:30

(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 :

Convert to date = 2022-01-01
Data type = <class 'datetime.date'>

(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 :

Today =  2023-04-09 10:41:51
Convert to string = 2023-04-09 Data Type = <class 'str'>

DOWNLOAD ERP

Latest Posts

Creating Notifications in Odoo 16 for Display

September 27, 2023

Members Module Features in Odoo 16

September 26, 2023

Create On-Site Payments & Picking With Odoo 16

September 19, 2023

Scalable ERP Software in Real Estate Services | Visit Us at the Gitex 2023

September 18, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *