How to create a Date variable in Elm - elm

I want to hardcode a date in a record in elm. The record signature is
type alias Record = { .., startDate : Date, .. }
On my code I am doing
record = { .., startDate = Date.fromString "2011/1/1", .. }
The problem is that the Record type expects a Date type but Date.fromString signature is
String -> Result.Result String Date.Date
How can I create the Date to use on the Record type?

You're getting the Result because there is a chance that parsing the string to a date failed. You can handle it one of 2 ways.
Ignore it
If you want to just say "I know this string will be valid date and I'm not worried that I may have messed it up" then you can just provide a default date
Date.fromString "2011/1/1" |> Result.withDefault (Date.fromTime 0)
This will leave you with a Date but will default to the unix epoch if the parse fails.
Use it
Think about what you would want to happen if the parse were to fail and handle it where the date is used. Ex. if you're displaying it as a string you could display the date or if the parse failed display "TBA".
Note: You may have noticed that Date.fromTime just returns a Date not a Result (because an Int can always be parsed to a Date). If you don't mind converting your dates to unix timestamps you could hardcode the timestamp and use that without having to deal with Results

Related

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)

PSQL function help - date format issue

Just trying to clean up some functions someone else has done in a postgres.
Would anyone know what the following does? It was working, but the date format changed when it started coming in as '1999-09-07 16:30:00.000'
I don't know what the previous format was.
select
case
when dbDate = '' then null
when dbDate != '^\d.*' then dbDate::timestamp
else '1900-01-01'::date + dbDate::int
end as dbDate
Whenever I call the function with the date it gives me
invalid input syntax for integer: "1999-09-07 16:30:00.000"
This nice function was taking multiple kinds of date inputs and normalizing them.
I assume it expected one of:
blank which it let be null
a date starting with 'MMM' in date format which would not pass '^\d.*' (i.e. something that doesn’t start with a number) which it would cast as a date
a number
The reason that the date was being casted as an INT is because after failing the first two tests the person writing this was expecting an INT. They wanted to add the integer to the beginning of Time (i.e. 1900-01-01) like Excel does.
1999-09-07 16:30:00.000 fails the second test even though it could be cast as time.
This passes through to the else, which fails to cast it as INT, and throws the error.
In this case, you need to change your second test. Make it something that will allow a datetime that you have coming in, but that would reject a number that should be added to 1900-01-01.
If you don’t think you will have numbers coming in that should be added to 1900-01-01, then just get rid of the third test and use
select
case
when dbDate = '' then null
else dbDate::timestamp
end as dbDate

Ms Access - How to verify the date time format displayed in yyyy/mm/dd HH/MM/SS

May I know could I verify whether the date displayed in the field is in specific format
As per my requirement, the datetime should be displayed in format 'yyyy/mm/dd HH(24hr)/MM/SS'
Eg: The valid value should be '2014/07/18 14:16:48'. If the date is displayed as '18/07/2014 14:16:48', then it is invalid.
Using query how I verify whether it is shown in the format which I have expected. I could use IsDate option to verify it is a valid date and also I could use Mid function to verify the date separator which is '/', but how could I verify the format.
Thanks
If the column is stored as Text, use the SQL Like operator. Select valid dates:
SELECT * FROM myTable WHERE myDate Like "####/##/## ##:##:##"
Select invalid dates
SELECT * FROM myTable WHERE myDate Not Like "####/##/## ##:##:##"
# stands for a single digit.
See Like Operator.
But note that this only makes sense if myDate is a Text! If the type of myDate is Date/Time, the date is stored in a numeric format internally and is only formatted as a date for display. So don't confuse the date value per se and the date as it is displayed.
A Date/Time is always stored in the same way, no matter how it is formatted and displayed!
All you need to do is to open the Table in Design View and to set the Format property.

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)

Storing result of date arithmetic in string

The following code is supposed to subtract 10 days from a given date, store the result in a string variable and write it.
DATA str TYPE string.
DATA date TYPE d.
date = '20130418'. " 2013-04-18
str = date - 10.
WRITE str.
I would expect the output to be 2013-04-08 or at least an unformated 20130408. But the actual output is a quite mysterious number which doesn't make sense to me at all:
734967
Can someone explain me where this number comes from?
I already found a workaround (just put the result in another variable of type d and then assign this variable to the string), but I am still interested in an explanation for this strange result.
SAP_BASIS release is 702.
Take a look at the conversion rules: For the substraction, the date is converted to the number of days since 01.01.0001 internally (source type date, target type I), then the arithmetics take place. IF the result is a date field, a conversion (source type I/Packed, target type D) back into the form YYYYMMDD is applied. However, the conversion I/Packed to string is defined differently - so the string contains the number of days between your result date and 01.01.0001.