Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Need to split month and year from date fields?

By
vadivel
on 8/13/15, 6:32 AM 1,450 views

Dear Friends,

How to split month and year from date field and fetch that result in char field.Please advice me guys to done this.

0

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 8/13/15, 7:19 AM

Try this friend:


import time

import datetime

from datetime import date

from datetime import datetime, date, time

ref_id = False

y = False

m = False

state = False

d = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

print d

d_ref = datetime.strptime("2011-01-11","%Y-%m-%d")

print d_ref

d_to = datetime.strptime("2012-01-31","%Y-%m-%d")

print d_to

if d_ref < d_to :

d_ref = d_to

y = d_to.year

m = d_to.month

day = d_to.day

print y

print m

print day

print d

print (str(y),str(m),str(01))

You can eliminate the day :)

Regards.

0

Baiju

--Baiju--
3154
| 5 4 8
Kochi, India
--Baiju--
ODOO technical and functional

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju
On 8/13/15, 7:07 AM

Hi,,

Try this script:-

date = datetime.strptime(date_field, DEFAULT_SERVER_DATE_FORMAT)
month = date.month
year = date.year

Hope this helps..

0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 8/13/15, 6:41 AM

Hi Vadivel,

From Date field you will get date as string,  first convert it to datetime object type using:
date = datetime.strptime([your string date], "%Y-%m-%d") 

then you can simply use datetime attributes to get month and year as:

" print date.month, print date.year......." 

To show it in another field you can use on_change() method of [your date] field.

Don't forgot to import datetime at top as;

"from datetime import datetime"

Hope it helps you !    

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 8/13/15, 6:32 AM
Seen: 1450 times
Last updated: 8/13/15, 7:19 AM