Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
9 Trả lời
42879 Lượt xem

hi,

please tell me how to change date format for email templates yyyy/mm/dd to dd/mm/yyyy format.

i changed the date format from settings->Translations->Languages but email template dates have no change and show dates in same old/default format yyyy/mm/dd.

Can anyone help me. Thanks!

Ảnh đại diện
Huỷ bỏ

Hey I am looking for the answer as well. Did you get any solution for this?

Hello, I am looking also for the answer.

Câu trả lời hay nhất

You can try with something like:

 ${format_tz(object.date_invoice, tz='UTC', format='%d-%m-%Y')}

Ảnh đại diện
Huỷ bỏ

Perfect fit with V11. Thanks

Câu trả lời hay nhất

There is the email_template_dateutil module in OCA/server-tools:

https://github.com/OCA/server-tools/tree/7.0/email_template_dateutil

This module adds the format_date filter in the email template environment, and can be used like this:

${object.date_invoice|format_date("%m/%d/%Y")}

${object.some_datetime|format_date()}

This will use the current user's timezone by default, or the server timezone, or you can pass in your own timezone.

Ảnh đại diện
Huỷ bỏ

Module tested and it works perfectly with any date and time format !!

Câu trả lời hay nhất

SIMPLE:

We Know the date format output by: ${object.date_invoice} (or any other date) in Email Templates is: YYYY-MM-DD.

We also know that Python sees the value as a string. Use the built in split() method to output each part of the date like so:

For DD-MM-YYYY formatted date:

Replace: ${object.date_invoice}

With: ${object.date_invoice.split('-')[2] + '-' + object.date_invoice.split('-')[1] + '-' + object.date_invoice.split('-')[0]}

No extending classes or writing custom modules!!

Ảnh đại diện
Huỷ bỏ

the problem with this, that you change to format. But you ignore if you are in a different timezone as UTC

I tried the above syntx in Odoo8 and it didn't work. But the following worked nicely: ${object.x_date_livraison.split('-')[2]}/${object.x_date_livraison.split('-')[1]}/${object.x_date_livraison.split('-')[0]}

Câu trả lời hay nhất

I like it though... In my case Format was YYYY-MM-DD hh:mm:ss Just wanted DD-MM-YYYY So ${(object.date.split('-')[2]).split(' ')[0] + '-' + object.date.split('-')[1] + '-' + object.date.split('-')[0]}

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

This worked for me in V13

${format_datetime(object.date_from, tz=object.employee_id.tz, dt_format='dd.MM.YYYY')}

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can find more details on the template language at http://jinja.pocoo.org/docs/dev/templates/#builtin-filters

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Oke, can you give an example of extending an object by a openerp module?

Because I would really like, to follow the correct way.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Only editing the template is not possible to fix this. Not only the format is wrong even the timezone is alway UTC and this is confusing if your are outside UTC.

A workaround is to defined a new field as function:

    def _get_date_for_email(self, cr, uid, ids, field_name, arg, context=None):
        reads = self.browse(cr, uid, ids, context)   
        result = {}     
        for obj in reads:        
            utc = datetime.strptime(obj.date, '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.timezone('UTC'))
            #FIXME: can we read timezone from context?            
            #if 'tz' in context:            
            #    to_zone = tz.gettz(context['tz'])
            #else:
            to_zone = tz.gettz('Europe/Berlin')
            meeting_date = utc.astimezone(to_zone)
            #TODO: Make formating here
            result[obj.id] = "%s" % meeting_date
        return result

    _columns = {
        'date_for_email' : fields.function(
            _get_date_for_email,
            type='char',
            readonly=True,
            string='Date Formated'),
    }

Maybe we need a more generic solution of this. The OpenERP Support offer me to develop a private patch but not merging to stable. I think a small community module should be the best way. So we will work on this.

Ảnh đại diện
Huỷ bỏ

Hallo Markus Thank you first for your trouble. At this solution, I am also interested. Sorry, but I do not know where I need to insert this code, or how I should use. Could you perhaps give here a small guide or help? Best thanks.

You need to extend the object which the email report is used. Like sale.order and write a addon which extends this object.

Oke, how can we extend the object so the email template is using the correct date syntax?

do we have to change the add-ons/email_template/email_template.py file for this?

No we extend the object by a openerp module, no core hacks involved

Oke, can you give an example of extending an object by a openerp module?

Because I would really like, to follow the correct way.

Câu trả lời hay nhất

I opened a bug about this on github:

https://github.com/odoo/odoo/issues/1108

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 8 24
74542
2
thg 10 24
971
0
thg 10 23
2060
2
thg 10 23
1936
0
thg 9 23
919