Get weekday id in odoo - odoo

I want get day id from my datetime field.
print(datetime.today().weekday()) --> return 4
my_datetime = self.start
print(my_datetime) return 2017-07-14 09:47:14
How replace datetime.today with my my_datetime?

Try this example, return day name:
my_datetime = self.start
day_name = datetime.strptime(my_datetime, '%Y-%m-%d %H:%M:%S')
print(day_name.strftime("%A"))

Use Odoo's built-in convert methods to do that:
from odoo import fields # usually already done for odoo models
if self.start:
day = fields.Datetime.from_string(self.start).weekday()

Related

Updating data when date is in condition

I am facing an issue to update the dataset with data type is date in Condition
Script:
UPDATE blse
SET employment = 2066.3
WHERE date = (i am not sure how)
AND industry = 'Total Nonfarm'
AND state = 'Alabama'
;
The Date type formate is YYYY-MM-DD
Example:
SELECT * FROM posts WHERE date = "1982-02-22";
so the command must be like
UPDATE blse
SET employment = 2066.3
WHERE
date = "2022-09-22" AND
industry = 'Total Nonfarm' AND
state = 'Alabama';
I would recommend to use to_date() if you are casting from a string to accommodate a specific format. Example:
SELECT to_date('2000/JUN','YYYY/MON'); -- ok
SELECT '2000/JUN'::date; -- error
Check doc on to_date function:
https://www.postgresql.org/docs/current/functions-formatting.html

list comprehension vs lambda function in pandas dataframe

I'm trying to convert decimal years to datetime format in Python. I've managed to make the conversion using a list comprehension, but I cannot get a lambda function working to do the same thing. What am I doing wrong? How can I use a lambda function to make this conversion?
from datetime import datetime, timedelta
import calendar
df = pd.DataFrame(data = [2021.3, 2021.6], columns = ['dec_date'])
# define a function to convert decimal dates to datetime
def convert_partial_year(number):
# round down to get the year
year = int(number)
# get the fractional year
year_fraction = number - year
# get the number of days in the given year
days_in_year = (365 + calendar.isleap(year))
# convert the fractional year into days
d = timedelta(days=year_fraction * days_in_year)
# convert the year into a date format
day_one = datetime(year, 1, 1)
# add the days into the year onto the date format
date = d + day_one
# return the result
return date
# my lambda function does not work
df.assign(
date = lambda x: convert_partial_year(x.dec_date)
)
# my list comprehension does work
df.assign(
date = [convert_partial_year(x) for x in df.dec_date]
)

Convert string to datetime in cosmos db

In my cosmos db a date field is stored as string like this
{
"ValidationWeekStartDay": "27-Apr-2020"
}
I have to write a query to extract all documents whose ValidationWeekStartDay is greater than current date. How can I achieve this in cosmos db query?
Select * from c wher c.ValidationWeekStartDay > GetCurrentDateTime ()
this does not give me correct result.
This is the problem with date format, the documents you are storing is in format 'dd-MMM-yyyy' while GetCurrentDateTime() function gets the date in format 'yyyy-mm-dd....'. So when you run the above query, comparison like below happens:
'27-Apr-2020' > '2020-08-17'
It compares the characters one by one and first 2 characters of first value becomes greater than second value. For testing purpose, anything above date 20 will be returned by your query irrespective of any month.
There are 2 ways to resolve this.
Store the date in same format as GetCurrentDateTime() function.
Create a udf like below. You can create your own udf, this is just a sample one based on date format.(pardon the formatting, you can copy and run it as it is)
function formatdatetime(datetime){ datetime = datetime.substring(7,11) + '-' + datetime.substring(3,6) + '-' + datetime.substring(0,2); datetime = datetime.replace('Jan','01'); datetime = datetime.replace('Feb','02'); datetime = datetime.replace('Mar','03'); datetime = datetime.replace('Apr','04'); datetime = datetime.replace('May','05'); datetime = datetime.replace('Jun','06'); datetime = datetime.replace('Jul','07'); datetime = datetime.replace('Aug','08'); datetime = datetime.replace('Sep','09'); datetime = datetime.replace('Oct','10'); datetime = datetime.replace('Nov','11'); datetime = datetime.replace('Dec','12'); return datetime; }
And then use the below query:
select c.stdDates as stdDates from c Where udf.formatdatetime(c.stdDates) > GetCurrentDateTime ()

How can I subtract datetime in Python?

I'm trying to make an age calculator for python and I have a problem with subtracting the user's input date of birth and today's date. I have tried float but it doesn't work. I tried subtracting the variables itself but that doesn't work, either.
age_str = input ("Enter your birthday on dd-mm-yy Format:")```
age = datetime.datetime.strptime(age_str, '%d-%m-%Y')```
today_str = datetime.date.today()```
today = datetime.datetime.strptime(today_str, '%d-%m-%Y')```
total = age - today```
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))

how to show date in expirydate column

how to show date in expirydate column which is 5 time more of the original date.
i.e. i have a column name created and have a deafult date(7/19/2023) but in expiry date column i want to show a date like (7/24/2013) always when i save the expiry date will be 5 more to the original date?
public function rules(){
return array(
array('title','required'),
array('jobid,notes,companyid,createdon,expirydate','safe'),
array('createdon','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'update'),
array('createdon,expirydate','default',
'value'=>new CDbExpression('NOW()'),
'setOnEmpty'=>false,'on'=>'insert')
);
}
You can use strtotime to calculate expirydate:
$time = strtotime('+5 days', strtotime($model->createdon));
$model->expirydate = date('m/d/Y', $time );