rails change date format before saving to mysql DB - mysql

I have a datepicker that I want to display in MM/DD/YYYY. However I'm using a mysql db which wants YYYY-MM-DD. As it stands right now its saving but the day and month are being reversed.
Right now I have an initalizer, date_time_formats.rb which has:
Date::DATE_FORMATS[:default] = '%m/%d/%Y'
Also in my datepicker jscript I have the correct format which I want to display:
$(this).datepicker({"format": "mm/dd/yyyy", "weekStart": 0, "autoclose": true, "startDate":new Date()});
I've tried things like this:
Date.strptime(params[:location], "%d/%m/%Y")
But I get a HashWithIndifferentAccess error.
How can I reformat the date in the params hash prior to assigning to an instance of the model? The reason being it seems to get rejected from the model if the date would be invalid (ex. April 4, 2013 is fine, but April 20, 2013 is not because there is no 20th month). I'm a big time rails novice so maybe I'm wrong but thats how it appears to be working. Thanks!

try the american_date gem located here: https://github.com/jeremyevans/ruby-american_date

Look up the strftime method of the various date classes. The incantation you want is Time.local(params[:location]).strftime('%m/%d/%Y'), hope that helps!

A quick way to sanitize the date to the correct format is to grab it in the controller before it gets saved and use strftime to correct the format. For example:
#model = Model.new(model_params)
#model.date_you_want = #model.date_you_want.strftime('%Y/%d/%m')
#model.save
This saves the date in the correct format. (I'm also using datepicker-boostrap gem)

Related

Laravel + Carbon + SQL time formatting problem

Can someone please assist me with the following problem. I am using Carbon to get the current time. Once I got the time and send it to my (myphpadmin) database it displays the whole date and time and not just the time. Here are all the code being used.
Laravel Code:
$date = Carbon::parse(now())->timezone('GMT+2');
$time = $date->toTimeString();
$UserRequest->finished_at = $time;
SQL Database layout and format:
Display: (Incorrect)
I have literally tried all the custom formatting from Carbon docs nothing sends over just the time.
I need this format -> 12:09 pm
Table Structure:
The problem more complicated than you describe.
First looks as finished_at is datetime field, so you can not store only date in this field. Sure, you can change the column format to time and store only time part. But this approach can cause problem with overdate date (started_at may be previous day or early).
So I think you need not change your storing flow, but you can change representation flow by using appropriate format

Can't get the Today() function to work properly in MS Access 2016 Custom Web App

I'm trying to setup a query that will show me all of the records in a particular table where the listed expiry date is either in the past or upcoming in the next 6 months (approximately).
At the moment, I have the "Expiry" field added to my query and the 'Criteria' as .
When I try to save the query, I get the following message:
Access can't evaluate an expression or convert data because data types aren't compatible with each other.
TECHNICAL DETAILS
Correlation ID: ae68949d-3041-3000-0984-71635f8fd670
Date and Time: 7/28/2016 6:54:34 PM
I've tried searching the web for a solution, but most websites refer to the Date() function that doesn't seem to be available in the Access 2016 Custom Web App. When I take out the "+180", it works fine but obviously doesn't give me what I need.
Any help would be appreciated.
=============================
UPDATE:
Some users have asked for my SQL and Table Design details. I don't seem to have any way of accessing the SQL View (the option doesn't appear), but here's a copy of my table view:
Access Query Table Design
In the table, 'Active' is a Yes/No field and 'Expiry' is Date/Time.
Try
< DateAdd(Day, 180, Today())
as criteria.
According to https://msdn.microsoft.com/en-us/library/office/jj249452.aspx this should work in a custom web app.
Error says that you have two different date types and they can't be compared. so, as the Today() returns only the Date with 12:00 pm, I can guess that your other "Expiry" Field is a datetime type. So, you can do either: use Format function to convert datetime to date like this
Format([2/22/2012 12:00 PM],"dd/mm/yyyy")
or use Now() function which returns datetime,
or Share your Code :)

SSRS Date value is not valid

A few weeks ago the guys who handle our servers upgraded from MSSQL2008 R2 to MSSQL2012 ServicePack 2
Now all the reports where we pass a date parameter from our application give us an error.
The date in the database is in the following format: "DD-MM-YYYY 00:00:00:0000"
So we changed the value we are passing from our application to fit this format, but the date is still invalid.
We don't debug on the server, so we installed all relevant versions locally and it worked fine.
Anybody else had this problem?
Yes, I have had that problem with a update from SQL2012 to SQL2014. But that was caused by the fact that SQL 2014 has more datefields. This is not relevant for you.
In your case though, I would change all the fields to this format:
"YYYY-MM-DD 00:00:00:0000". You can reformat it in the report if you want it to be shown like "DD-MM-YYYY 00:00:00:0000"
I suspect it has something to do with regional settings somewehere. When using YYYY-MM-DD instead of DD-MM-YYYY you avoid al that sh*t.
I always use YYYY-MM-DD to avoid confusing.

dd/mm/yyyy date format in SSRS

i'm trying to specify dd/mm/yyyy dateformat for date/time parameter in SSRS 2008 R2.
My computers datetime format is mm-dd-yyyy.
My requirement is, i want to show date format at dd/mm/yyyy irrespective of the system/server date format.
I've already tried CDate(Format(Today,"dd/mm/yyyy")) which didn't work. one very strange thing i observed is, it shows dd/mm/yyyy format only for dates on or before 12-MM-yyyy, and 13 onwards it gives error: Conversion from string '25-04-2014' to type Date is not valid. (Possibly it is trying to map 25(daypart) with MM-dd-yyyy (month part)) which is out of range of total months i.e. 12)
my research on internet says it is a bug in BIDS 2008.
What do i do to display date as dd/mm/yyyy ??
I don't have enough reputation to comment, but I did notice that you failed to put "()" after "Today". If I'm not mistaken you must put Today() for that function to work. Also, you might want to try putting CDate Around the Today() function. You shouldn't need it, but it's worth a shot. Also, for some odd reason, in my experience, you must capitalize MM for format to work correctly.
Like #Aditaya said, it should be =format(Today(),"dd/MM/yyyy")
The expression I usually use is:
=FormatDateTime(Fields!Date.Value, DateFormat.ShortDate)
However, this may be region specific.
Rather than writing an expression to do the formatting, you can also use the Textbox Format Property. But first you need to make sure that the data is in a date format. So use the CDate function on your column like this:
=CDate(Fields!Date.Value)
Then in the textbox properties go to the Number tab. Select Date for the category. Then you can select whichever format you want or use a Custom format. This will change how the column displays when you run the report.

Calender on webpage to MySQL database

I would like to make a calender schedule for my work. That means that everyday I work I will put it in a form, where it is connected to my MySQL databases. From a page I would like to see how many hours I had in a period. I was thinking that there was 2 of these icons:
http://www.calendar123.org/calendar123/november-2012-calendar-printable-16.jpg
where I could those a period, and get it displayed. I have the connection to my database etc, but how do I incorporate a calendar function like the picture on my webpage, where I can choose dates from my database? I am not familar with PHP, but I am working with html, xml, java, javascript, css and MySQL
Hope somebody can help me.
Best Regards
Mads
You'll probably want to use the jQuery datepicker but there are tons of them out there.
UPDATE:
It acts like a textbox on the PHP side. You'll get a string date in your $_REQUEST array. You'll want to use the strtotime() function to convert that into a PHP timestamp. Then use the date function to convert that back into a MySQL formatted date like so:
$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_REQUEST['date_html_id'])));
Now $date can be inserted directly into a MySQL DATE or DATETIME field.