I was about to implement a search function where users can filter a datetime column.
I am currently using rails datetime_field_tag however I can't seem to make it work with ransack where people can choose a date with time in hours and minutes then either AM or PM.
Users can enter datetime in this format: dd/mm/yyyy hh:mm AM/PM
The column that i am searching for is in datetime format, ex: 2021-05-12 09:09:48
What's the best way to implement this one? with ransack or not? and how?
just my first time implementing datetime search function with ransack,
I tried searching online but i cant seem to find a good solution.
thanks.
If you're receiving the datetime in string format from parameters and directly passing it to ransack, then you should convert that string to 24-hour format string (db format can also be used) first and then pass it to ransack.
For example
query_value = Time.zone.parse(params[:created_at_filter]).to_s(:db)
Person.ransack(created_at_eq: query_value)
Related
My Ionic 3 app uses the ion DateTime field where the user inputs a date.
<ion-datetime displayFormat="DD/MM/YYYY HH:mm" [(ngModel)]="scheduled_at"></ion-datetime>
This gets then send to my Rails 5 API. The string that arrives at my API endpoint looks like this:
"scheduled_at"=>"2018-06-14T23:33:42.939Z"
I read that the Z indicates UTC (https://robots.thoughtbot.com/its-about-time-zones). The issue is that the timezone the user enters is not in ZTC but in 'America/Sao_Paulo', so when I parse the string to a DateTime object in Rails and store it to the DB it will always be wrong.
Is there any way to tell the ion DateTime the correct timezone?
Maybe, there is no way to do that. moment.js is an alternative.
In ionic datatime documentation,
Advanced Datetime Validation and Manipulation
The datetime picker provides the simplicity of selecting an exact
format, and persists the datetime values as a string using the
standardized ISO 8601 datetime format. However, it's important to note
that ion-datetime does not attempt to solve all situtations when
validating and manipulating datetime values. If datetime values need
to be parsed from a certain format, or manipulated (such as adding 5
days to a date, subtracting 30 minutes, etc.), or even formatting data
to a specific locale, then we highly recommend using moment.js to
"Parse, validate, manipulate, and display dates in JavaScript".
Moment.js has quickly become our goto standard when dealing with
datetimes within JavaScript, but Ionic does not prepackage this
dependency since most apps will not require it, and its locale
configuration should be decided by the end-developer.
Try to use toLocaleString() string function of Date
myDate: String = new Date().toLocaleString();
I need to parse a date in Rails from a JSON string that could possibly be provided without a month or day, and return it in ISO8601 format. If a day is not provided, then I need to return the first of the month of the year, and if a month is not provided, the first of the year.
Simply running DateTime.parse(response['date']).utc.iso8601 throws an ArgumentError when the month or day is missing, such as DateTime.parse('2001').
DateTime handles my exact conditions beautifully with DateTime.new with optional integer parameters, but how can I best handle this when parsing a string?
I have to keep i18n in mind when doing this, so while I could store three diferent formatted string args for strptime for each locale, that seems hacky. If that is indeed the best way to handle this, I'd love an explanation.
I know there was a lot of discussion about datetime parsing in dygraphs and problems with Javascript. But still, I have a problem how to correctly pass datetime.
I'm using dygraph to show data (points with 5 seconds interval) and it's crucial to have correct datetime. But if I pass datetime as timestamp (1401580800) or as ISO 8601 (2014-06-01T00:00:00+00:00), I always get datetime modified to my local time.
My question is, how to correctly pass the datetime to dygraph so datetime doesn't change?
Dygraphs use Javascript Dates, which display according to your local timezone (unfortunately, there's no way to display them using an arbitrary timezone).
One solution is to modify the data - I've encountered a similar problem in my own work, and I made a small helper function to make fake Javascript UTC dates (basically ending up with a Javascript date that's actually the wrong time but shows up looking correct when displayed in local time). This worked for my application, which used moments (http://momentjs.com/) to represent dates everywhere unless required by some other library to use Javascript's Dates.
Another (probably more correct) solution is to modify Dygraph's functions to display the right things, as demonstrated in the answer to this post: Dygraph showing dates in an arbitrary Timezone
The JavaScript Date object will always use the local time of the computer it's running on. If you don't want that behavior, you'll need to use something else. Consider building a pre-formatted string using something like moment.js and then just pass the string to dygraphs instead of the date.
I'm working on an app that will allow users to search for any recordings we have in a database, organized by three values: agent (name), phone, and date. date is recorded as a datetime value. I want to provide advanced search features that will allow users to select time ranges following this guideline:
Year: entered into a text field
Month: entered into a text field
Day: entered into a text field
Hours: two select lists representing a range upper and lower bound
Minutes: two select lists representing a range upper and lower bound
I watched this Railscast for help on how to get started with the search logic, but I'm concerned because I don't know how to pass in dateparts into Rails queries. Normally you would run something like select date_part(hh, date) as date_hour from table_name to get the value of hour, but how would you do that in Rails, or in the way Ryan Bates suggests in his Railscast?
You may concat the values in the controller accessing them by the params and then parse to a datetime
Then DateTime.strptime allows you to specify the format and convert a String to a DateTime.
Apologies if the title is not clear, I' not really sure how to explain this clearly.
In a Rails app I'm pulling in users' data from a 3rd party provider's API. One of the attributes is the user's birthday.
I am storing the hashed data in my database and, in certain circumstances, need to display the user's birthday in the view.
The problem is that the birthday is not formatted as a Date. Within the hash, it is in the format mm/dd/yyyy. This means that my usual date scoped formats don't work on it.
Given that I am extracting the birthday from the hashed data column as follows
<%= #user.hashed_data["info"]["birthday"] %>
what is the best/ most efficient way to handle this so that I can display localized date formats?
Do I need to split on the / symbols, and then recombine the resulting string to a date? Or am I overcomplicating this?
Thanks for any suggestions.
Try:
Date.strptime('01/31/2011', '%m/%d/%Y')
# => #<Date: 2011-01-31 (4911185/2,0,2299161)>
The simplest way to convert plain text to date or datetime is to use the chronic gem.
A very usefull parameter that you can pass to the chronic parser is the :endian_precedence . See my answer here for more details.
Regards, Dorian