2
votes

How can I extract only the year from a date field (x_date_invoice) to a new computed odoo field for example x_jaar

I am using Odoo 10. FIeld x_jaar info Field x_date_invoice info

4
hello Pelingier, doing import datetime file?Er CEO Vora Mayur
first import this below,Er CEO Vora Mayur
(1)from datetime import datetime (2)import timeEr CEO Vora Mayur
ValueError: forbidden opcode(s) in u'from datetime import datetime \nimport time\nfor record in self:\n record[\'x_jaar\'] = str(datetime.strptime(record[\'x_date_invoice\'],"%d/%m/%Y %H:%M:%S").strftime("%Y"))' (error that i receive)Pelingier
can you please paste code here?Er CEO Vora Mayur

4 Answers

1
votes

Hello Pelingier,

Basic Function of Python,

strptime()

Description The method strptime() parses a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime().

The format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime().

If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.

Syntax
Following is the syntax for strptime() method:

time.strptime(string[, format])

Parameters
string -- This is the time in string format which would be parsed based on the given format.
format -- This is the directive which would be used to parse the given string.

strftime()

Description The method strftime() converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument.

If t is not provided, the current time as returned by localtime() is used. format must be a string. An exception ValueError is raised if any field in t is outside of the allowed range.

Syntax
Following is the syntax for strftime() method:

time.strftime(format[, t])

Parameters
t -- This is the time in number of seconds to be formatted.
format -- This is the directive which would be used to format given time. The following directives can be embedded in the format string:

==========================================================================

Your Question Solution

The first import library is from datetime import datetime after use all the functionality of the DateTime.

Try below code,

for record in self:
    record['x_jaar'] = str(datetime.strptime(record['x_date_invoice'],"%d/%m/%Y %H:%M:%S").strftime("%Y"))

I hope my answer is helpful.
If any query so comments, please.

1
votes

fields.Datetime and fields.Date are storaged as string, so yo have to parse into a python datetime. Then you can extract the year.

datetime.strptime(your_datetime, your_datetime_format).year
0
votes

In odoo, datetime is imported using :

import datetime

Not the usual way:

from datetime import datetime

That said, you should use this code:

str(datetime.datetime.strptime(record['x_date_invoice'],"%Y-%m-%d %H:%M:%S").strftime("%Y"))
0
votes

You neeed to use from_string mathod to get a datetime object than use it to get the year:

for record in self:
    x_jaar = fields.Datetime.from_string(record.x_date_invoice).year