Skip to Content
Menu
This question has been flagged
4 Replies
12641 Views

Hi there, 

ist there an easy way to set a date field to the first of January of the current year ?

As i know i can set a date field to the current date:

Kind regards,

Franz

date_to = fields.Date(string="End Date", default=fields.Date.today)
Avatar
Discard
Author

Hello David,

it works like a charm, thank you so much !

The other solution is also interesting for me !

Franz

Best Answer

Hello

import time

date_to = fields.Date(string="End Date", default=time.strftime('%Y-01-01'))
Avatar
Discard

Hi David,

Your solution, save my time.

Thank You +1

Best Answer

Hello Franz,

Get current year and append to field's default value

current_year = datetime.now().year
date_field = fields.Date("Date Field",default=datetime.strptime('%s-01-01' % (current_year),'%Y-%m-%d'))

Thanks,

Avatar
Discard