Right now I'm using a datetimepicker to make it easy for users to select an exact date/time and present that in a readable format. However, rails does not like that format and can't Time.parse it.
I've tried out the Chronic gem, and it's close, but it still can't parse something like:
02/27/2013 08:36:57 PM
How can I make user inputted time into an acceptable format for my database? (mongo)
I'm using this gem: https://github.com/asgeo1/bootstrap-datetimepicker-rails
based on this datetimepicker: https://github.com/tarruda/bootstrap-datetimepicker
demonstrated here: http://tarruda.github.com/bootstrap-datetimepicker/
Check out the timeliness gem:
https://github.com/adzap/timeliness
The date picker will always return the value in a standard format, so you can parse it.
#02/27/2013 08:36:57 PM
month = input[0..1]
date = input[3..4]
year = input[6..9]
datetime = DateTime.civil(input[6..9], input[0..1], input[3..4],
input[11..12], input[14..15], input[17..18])
Related
I have a form submitting to a controller action with a field that is in the "%B %d, %Y" date format. Our site has recently added multi-language support and now this is breaking when we translate the text in the field to non-english.
Example in ENGLISH: (This works as expected and populates the field with a date)
#model.datetime_field = "May 01, 2016"
Example in GERMAN. (This will not assign the field with a date)
#model.datetime_field = "Mai 01, 2016"
I've looked into the ActiveRecord source code and also at a few gems (like Alchemy) and I haven't found this problem to be solved. Please don't suggest that I change the form value, because due to the nature of the application/business requirements it needs to stay as is. Also to note, I need the solution to work for mass-assignment, e.g. #model.save(params[:model]) and #model.update(params[:model])
Does anyone know if there is something in Rails/rails-18n/3rd-party gem that solves this problem or do I have to write my own custom parser?
Thanks in advance for your suggestions.
This gem looks like it does what you want:
https://github.com/clemens/delocalize
I need to save Date Time in the (oracle) database in one column, which is sqlType of timestamp (looks like 01-JAN-14 12.00.00.000000 AM). While learning grails I've been using the Joda lib with it's "time picker".
The Joda timepicker has worked well, but now that I'm looking to go primetime I'm looking for something a little more user friendly. Frankly, text boxes might be more user friendly than the drops downs joda gives you.
Anyway, I'd like to remove joda and use something like this:
http://trentrichardson.com/examples/timepicker/
but I can't figure out how to implement it in grails. In my view, if I put:
<input type="text" name="endDate" id="endDate" value="${exampleInstance?.endDate}" />
in place of the g:datePicker, it works fine (the picker that is), except nothing gets saved to the database, and no errors are generated. I hit Save and the Show view comes up with an empty endDate field. Do I need more input tags?
Is there some easy way to implement a modern looking date+time picker that I've missed?
Furthermore, I see there is a plugin for this picker here
http://grails.org/plugin/jquery-ui-timepicker
But being that there isn't any documentation, I'm not sure how to use that either (?)
ANSWER
in controller save/update put something like:
def endDate = params.date('endDate', 'yy-MM-dd h:mm')
//println "Date from Picker was "+endDate
params.endDate = endDate
No further casting was necessary being that it ended up I could format the datepicker control to a very close format as what's in the database, but had I needed to cast from one odd format, or a string, to another, I toyed with this code, which is more psuedo than anything as I was thinking through the process (I'm sure there's a totally Groovy way to do this same thing):
SimpleDateFormat inputFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss.S");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy h.mm.ss.S a");
def v = params.endDate
Date date = inputFormat.parse(v);
String temp = sdf.format(date)
Date dateOut = sdf.parse(temp)
println dateOut
The datepicker, is your UI component therefore, you can have any library that you wish for UI and anything else for back-end. Mostly they are easy to implement, if they provide a little bit of documentation!!.
The timepicker for jQuery ui plugin, that you provided the link, is exposing a resource called jqueryUiTimePicker which depends on jQuery and jQuery-ui. So simply by including this resource into you resources configuration you should be able to utilize it. Its no different than defining your own resource and use it.
About saving issue that you have, on your save pass parameter failOnError:true so you can see the errors if any.
I have created a sample project that utilizes this plugin hope it helps
In your controller, you will need to parse the parameter value to a Date value. Something like,
def endDate = params.date('endDate', 'dd-MM-yyyy')
dd-MM-yyyy is whatever format the jquery plugin submits the date value. (println params for this or look up the plugin documentation)
If you want a date format binding to happen automatically, check the Date Formats For Data Binding in the doc http://grails.org/doc/latest/guide/single.html#dataBinding for a way to globally specify the format
I am still new to Ruby and am working on a project that involves users logging in through different calendar services, then displays it on a custom calendar on screen, for activity booking purposes.
I have been able to get the date in a String format. I am using the calendar I created after following the Railscast #231 to display the user's events. However, this calendar requires a date format to display the data.
I have tried everything I can, from Time.parse("start"), which gave me an error of:
"no implicit conversion of Date into String"
Thanks for any help in advance.I have a feeling I am going about this completely wrong. But I am so close to getting it to work I can taste it. haha
If you want to convert a string to a date use string.to_date.
Examples:
Time.now.to_s
=> "2014-02-15 04:07:13 +0000"
Time.now.to_s.to_date
=> Sat, 15 Feb 2014
I am sitting on a really strange issue with grails and the js fullcalendar.
I have users which have their own timezones stored in the database e.g. ECT
When users insert their events all dates are stored in UTC. This is set in the Bootstrap with:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
I heard its a good idea to store all dates in UTC. So when a german user is entering 1.12.2012 08:00 its stored with 1.12.2012 07:00. This is fine.
Now when I want to display those values from the db into the calendar the timezone is not considered by fullcalendar(i set ignoreTimezone: false) so the dates are comming over json with 2012-12-01 08:00+01:00 but its display as 08:00 am and not 07:00. What am I doing wrong here? this issue is driving me really crazy.
Can you check in by manually adding the event in javascript?I guess the problem is with the JSON format. The parseISO8601 function inside fullcalendar.js expects your object to be in some format and so if your format is not correct,then you have to experiment and see what works.
Quoted from fullcalendar website.
When specifying Event Objects for events or eventSources, you may
specify a string in IETF format (ex: "Wed, 18 Oct 2009 13:00:00 EST"),
a string in ISO8601 format (ex: "2009-11-05T13:15:30Z") or a UNIX
timestamp.
I have a standard Jquery date picker which shows 2 textboxes.
One textbox has the date like so: 15/11/2011.
The other textbox shows the time like so: 13:15.
Combining the values will give me 15/11/2011 13:15.
I want to be able to render an event using fullcalendar using that string (15/11/2011 13:15).
I'm stumped whether to use a jquery date picker format function, somehow squeeze this into a new Js data object or use some date library to do the conversion.
What is the best way to achieve it? When passing the date to fullcalendar plugin it states the date object must be like so:
*
When specifying Event Objects for events or eventSources, you may
specify a string in IETF format (ex: "Wed, 18 Oct 2009 13:00:00 EST"),
a string in ISO8601 format (ex: "2009-11-05T13:15:30Z") or a UNIX
timestamp.
*
For now I've basically sent the date to the server for persistence and then re-fetch the events. I let FullCalendar work out the date by pulling the date from a Json list. This saves having to trying to convert the date and then add it to the calendar on an individual event basis.