Shravy
Shravy

Reputation: 666

How to assign current date to a date field in odoo v8?

I wanted to assign the current date to a date field 'start_date' in the following code:

calendar_obj.create(cr,uid,
             {'name' : rec_res.act_ion,
              'user_id' : rec_res.asgnd_to.id,
              'start_date' : lambda *a:datetime.today().strftime('%m-%d-%Y'),
              'stop_date' : rec_res.due_date,
              'allday' : True,
              'partner_ids' : [(6,0, [rec_res.asgnd_to.partner_id.id])]
             },
context=context)

How to set or assign the current date value to the start_date field ?

Upvotes: 3

Views: 20905

Answers (5)

Bhautik
Bhautik

Reputation: 21

from datetime import datetime

current_date = fields.Date(string='Date',default=datetime.now())

current_date.strftime("%d-%M-%Y") // strftime function use for the Date Format

Upvotes: 1

python datetime – Date/time value manipulation.

Purpose: The datetime module includes functions and classes for doing date and time parsing, formatting, and arithmetic.

Available In: 2.3 and later

Use the datetime class to hold values consisting of both date and time components. As with date, there are several convenient class methods to make creating datetime instances from other common values.

'start_date' : datetime.datetime.now().strftime('%m-%d-%Y')


import datetime

print 'Now    :', datetime.datetime.now()
print 'Today  :', datetime.datetime.today()
print 'UTC Now:', datetime.datetime.utcnow()

d = datetime.datetime.now()
for attr in [ 'year', 'month', 'day', 'hour', 'minute', 'second', 'microsecond']:
    print attr, ':', getattr(d, attr)

Result:

Now    : 2015-08-14 16:27:51.475070
Today  : 2015-08-14 16:27:51.475462
UTC Now: 2015-08-14 10:57:51.475585
year : 2015
month : 8
day : 14
hour : 16
minute : 27
second : 51
microsecond : 475645

Formatting and Parsing

The default string representation of a datetime object uses the ISO 8601 format (YYYY-MM-DDTHH:MM:SS.mmmmmm). Alternate formats can be generated using strftime(). Similarly, if your input data includes timestamp values parsable with time.strptime(), then datetime.strptime() is a convenient way to convert them to datetime instances.

import datetime

format = "%a %b %d %H:%M:%S %Y"

today = datetime.datetime.today()
print 'ISO     :', today

s = today.strftime(format)
print 'strftime:', s

d = datetime.datetime.strptime(s, format)
print 'strptime:', d.strftime(format)

Result:

ISO     : 2015-08-14 16:32:23.914699
strftime: Fri Aug 14 16:32:23 2015
strptime: Fri Aug 14 16:32:23 2015

Upvotes: 2

Ludwik Trammer
Ludwik Trammer

Reputation: 25072

The Date field includes a today() method, just for cases like this:

'start_date': fields.Date.today(),

Of course you need to import fields first:

from openerp import fields

Upvotes: 13

import time

'start_date':time.strftime("%d/%m/%Y")

Upvotes: 3

ChesuCR
ChesuCR

Reputation: 9670

Try just

'start_date' : '%m-%d-%Y' % datetime.today(),

or

'start_date' : datetime.today().strftime('%m-%d-%Y'),

Upvotes: 1

Related Questions