Datetime issue in odoo10? - odoo

in my custom model I defined the fields
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
this is my compute function
#api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
This is success fully prints time difference.
Time from and time to are mm/dd/yyyy hh:mm:ss format.
How to change this to mm/dd/yyyy hh:mm:ss format
I changed the format like this '%Y-%d-%m %H:%M:%S' .
but it is not getting correct result.
How to change the format?
is it possible to calculate time difference in this format?

The Date and Datetime are saved as strings in the database in a certain format (see the class definition on fields.py. You cannot change the format that is used for the fields in the ORM. If you want to change the format of the date or datetime when you show these fields you can do it not from the code but from:
1) Settings -> Translations -> Find your language and inside you can change the way the fields Date and Datetime are rendered on the client side.
2) If you have a template/report you can use for example<p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" /> or another expression you want to change how the date or datetime will be formed.
3) In the field definition in your xml files you can use custom javascript/widget that will do the rendering.

Related

Date format value not updating from previous format to new format after update

When I update a column from a table with a date format of MMM DD,YYYY to a new format. The format doesn't change to the desired format which is YYYY-MM-DD HH:MM:SS. Even when update different value like GETDATE(). The date will change but the format will remain the same.
Current value & format from column the type is varchar
DueDate
Jun 27 2020 12:00AM
Desired format
DueDate
2020-06-27 00:00:00.000
Update statement
update TableName
set
DueDate = CAST([DueDate] AS smalldatetime),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
There is a huge confusion between "Date displaying format" and "Date storing format". The VERY short explanation is that what you mentioned is only a client side displaying format, while SQL Server have specific format which is used for storing dates (remember that the server stores zero and one only).
You can insert dates to a table using different styles (the official name for the displaying format is STYLE), and you can present the dates in the client side using different style, but it will always be stored the same from the "SQL Server point of view" according to the DATE type which is used.
In order to solve your original needs, all that you needed to do is to provide the server the information about the style which you use in the client side (in the query). This is done by using explicit CONVERT with the third parameter, which is the STYLE.
For example if you use in the client side an Israeli format like dd/MM/yyyy, then you should use CONVERT(DATE, '27/02/2021', 103).
For more information on different STYLEs you can check this documentation.
Note: If you want to display the dates in specific format which is not covered by the existing STYLEs then you can use the function FORMAT() in your query. This function is fully flexible to return the data in your specific format. Remember that this function returns the data as string and it will not be date anymore.
For example, let's say that I want to use the format: "Day:dd,Month:MM,Year:yyyy". So if the date is '27/02/2021' then I expect to get "Day:27,Month:02,Year:2021". In this case use below:
DECLARE #D DATE
SET #D = CONVERT(DATE, '27/02/2021', 103) -- convert string to date for storing
select FORMAT(#D, 'Day:dd, Month:MM, Year:yyyy') -- convert date to string for displaying
Solution use the format function
update TableName
set
DueDate = FORMAT (CAST([DueDate] AS smalldatetime),'yyyy-MM-dd HH:mm:ss'),
LastSyncDateTime = GETDATE()
where CaseGuid = 'DA2CE6A1-0394-463E-8E8D-962F3A24ADC8'
https://www.sqlshack.com/a-comprehensive-guide-to-the-sql-format-function/

java.sql.SQLException: ORA-01843: not a valid month error - date format conversion issue

I'm having a strange issue here with a query from which I'm trying to pull data queried by start and end date parameters.
I'm conducting the following.
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String preparedQuery = "SELECT ID, FULNAME, FRMEMAIL, LOCATION,TYPE1,TYPE2,BORROWER_NAME,LOAN_NUM,TIME_REQ
FROM FORM_REDUCVU WHERE to_date(TIME_REQ,'yyyy-mm-dd') >= ? AND to_date (TIME_REQ,'yyyy-mm-dd') <= ? ORDER BY ID DESC";
java.util.Date util_StartDate = sdf.parse( request.getParameter("time_req_date") );
java.sql.Date timreq_datevar1 = new java.sql.Date( util_StartDate.getTime() );
java.util.Date util_EndDate = sdf.parse( request.getParameter("time_req_date2") );
java.sql.Date timreq_datevar2 = new java.sql.Date( util_EndDate.getTime() );
out.println("datestamp java time vals "+timreq_datevar1 + " " + timreq_datevar2);
PreparedStatement prepstmt = connection.prepareStatement(preparedQuery);
prepstmt.setDate(1, timreq_datevar1);
prepstmt.setDate(2, timreq_datevar2);
And the out.print values will give something like 2018-10-09 and 2019-02-20.
This does match the date picker selections I'm making, which are in mm/dd/yyyy format, i.e. 10/09/2018 and 02/20/2019.
Now the above returns no errors in the log, but no data either. But I did have the SQL query as "
WHERE to_datechar(TIME_REQ,'mm/dd/yyyy') >= ? AND to_char(TIME_REQ,'mm/dd/yyyy') <= ?
When changing to this above, I get the error in the topic of not a valid month.
I would have thought the SimpleDateFormat class would have parsed the date to mm/dd/yyyy in pattern, but it seems to make the java.slq.Date object pattern hyphenated, like yyyy-mm-dd.
Am I not parsing it correctly? Or is this going to require a different class or package, like java.Time? I would thought this would work, but there has to be some small conversion method not working.
Bottom line - I'd really prefer to keep the jQuery date pickers with their mm/dd/yyyy formats in tact, and be able to query by these entries.
Any input regarding this is welcomed.
Thank you!
Do NOT use to_date() on a column that is alread a DATE to_date() will convert the date to a varchar just to convert it back to a DATE which it was to begin with.
So your condition should be:
WHERE TIME_REQ >= ? AND TIME_REQ <= ?
You are correctly using setDate() so Oracle performs a date to date comparison - there is no need to convert the date in the database to a string value.
Ideally you should use java.time.LocalDate and setObject() instead of java.sql.Date though (however not all Oracle driver versions support that)

How to convert YYYY-MM-DD HH:MM:SS TO MM-DD-YYYY HH:MM:SS IN SQL Server?

How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.
Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49
This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.
SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' +
convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
Use this Link as Reference for Date & Time conversion Formats
Try this
SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')
DECLARE #date DATETIME = '2017-12-18 18:16:49'
SELECT FORMAT(#date,'dd-MM-yyyy HH:mm:ss')
I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data
for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:
<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;
the output will be: 05-18-2018 05:00:34
You could take reference in this older discussion
This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again
Hope this will be help

VB.Net Custom Mask for Date/Time AM/PM

Here's what I'm trying to do.... I have a field in my application where I am to capture datetime something like this 11/02/2016 12:19 PM (example). I have a field in SQL Server with datatype = smallDatetime. It gets saved like this 2016-11-02 12:19:00. In my app I set the CUSTOM mask to 00/00/0000 90:00 am. Now what I'm trying to do is populate this saved date into my masked textbox but it does not look proper. Is there a way for me to format is somehow so when I try to populate the field in my application it looks properly? This is what I've been trying to figure out...
If Not IsDBNull(Dt("SavedOn")) Then
txtSavedOn.Text = Format(Dt("SavedOn"), "mm/dd/yyyy hh:mm tt"))
End If
Wha thappens is the PM/AM part gets displayed incorrectly and it only shows the M and a number instead of p/a
Change your mask to
00/00/0000 90:00 aa
Also your date format
txtSavedOn.Text = Format(Dt("SavedOn"), "MM/dd/yyyy hh:mm tt"))
(note the capital MM for month, opposed to lower mm for minute)
Just use this code
txtSavedOn.Text = Dt("SavedOn").tostring("MM/dd/yyyy hh:mm tt")

how to check linq comparing date format in vb.net

Say I want record of all employees of date "07/03/2013" where format is "MM/dd/yyyy".
my expression in linq will be :
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date="07/03/2013")
here how linq manage the date format as "MM/dd/yyyy". OR how to compare "f.Date" with MM/dd/yyyy ?
Say,
if i does
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date.ToString("MM/dd/yyyy")="07/03/2013")
It does not allow me. suppose my date will "07/13/2013" then we can consider it matches and sync the format with "f.Date" but what about date is under 12 ? In fact by using first expression, m getting march month records, rather July.
How to figure out this issue?
You are comparing string representations of the date/time, which depend on the current system's formatting options. Use a date literal (must be in 'M/d/yyyy') to create a new DateTime object:
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date=#3/7/2013#)
If it's a variable, say dte:
_dbContext.EmployeeDetails.Where(Function(f) f.EmpId = _empId And f.Date=dte)
It should be this:
_dbContext.EmployeeDetails.Where
(Function(f) f.EmpId = _empId And
f.Date.ToString("dd/MM/yyyy")="07/03/2013")
You where passing Month first, so it parses March. Change toString order so you pass days before and then month. (July)