We have a new field being added to our app where the client wants to be able to put in Sept 22. The input will be part of an import with 100 or so records. I know there are many libraries for parsing it but we want to be able to validate it. In case someone were to make a typo. Any thoughts or libraries to do this?
DateTime.parse will parse "Sept 22" with current year.
you can just make a dateTime with specified year as
date = DateTime.parse("Sept 22")
date_time_with_year = DateTime.new(year, date.month, date.day)
Check out Chronic
You can do things like
Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
#=> Sat May 27 12:00:00 PDT 2000
It will attempt to guess what the string was trying to convey, by default (ie: "Sept 27" will actually parse to something like 2013-09-27 12:00:00 -0500
Related
I have a date format like this "18/12/15" as input (it is a string)
18 => day
12 => month
15 => year (2015)
I am trying to store it in my db in a date column but it doesnt save the proper date (for the previous exemple I get Sat, 15 Dec 2018 whereas I should get something like day, 18 Dec 2015.)
How can I fix that
You could use strptime:
require 'date'
Date.strptime('18/12/15', '%d/%m/%y')
#<Date: 2015-12-18 ((2457375j,0s,0n),+0s,2299161j)>
You want to define a date with the order of day, month, year, while, due to the locale your intention is interpreted as year, month, day.
You can do one of the following:
pass the parameters in inverted order
implement a method which gets the parameters in the correct order and inverts them
set the locale to the one you intend to use
use strptime, as spickermann described
For a project I am working on I receive date and time in this format:
2015-08-16 15:00:00 UTC
yyyy-mm-dd hh-mm-ss UTC
How can I make the time display as "Saturday, August 16th 2015 at 3:30PM"? ("15:00" would be fine as well.)
And how would I make it so it checks if the date has already passed or not, so that it only displays dates that have not passed?
How would I make it so I can so that the time display as "Saturday, August 16th 2015 at 3:30PM (15:00 would be fine as well)?
Time.parse('2015-10-20 15:23 UTC').strftime('%A, %B %dth %Y at %l:%M%p')
#=> "Tuesday, October 20th 2015 at 3:23PM"
You might have to tweak it a bit to fix the suffixes (1st, 2nd, 3rd, etc)
And how would I make it so it checks if the date has already pasted or not?
You could do it like this (I'm sure there's a simpler way):
EDIT: Yes, there is a much simpler way -- check Matt's answer.
require 'time'
if Time.parse(my_date).to_i - Time.now.to_i > 0
# my_date is in the future.
end
To start, convert your string to a Time object via Time.parse(string) (APIDock).
After that you have all of the Time class to play with.
time.strftime
time.past?
I'm using MongoDB and Mongoid in my project. I have some difficulties with ordering "Time" field data.
As you know, eventhough you set a field type as Time inside your Mongoid Document, time data is stored as this format (note that I use +2 hours offset to get my own timezone's time instead of UTC):
Mon, 31 Dec 2012 08:30:00 EET +02:00
I'm not sure if I'm missing something but this is what happens to me.
I want to use that Time data inside one of my views. I need to sort some related stuff by ONLY Hour:Minute format. I don't want Rails to take care of the DATE part of the field data. Because whenever a new Time record inserted to DB, it takes the day info as CURRENT DAY.
Issue is:
Because of it saves CURRENT DAY for each of new Time records, when I try to order_by("hour DESC") NEW records always retrieved first eventhough HOUR part of the data is bigger!
For example:
First data:
=> Tue, 27 Nov 2012 19:50:00 EET +02:00
Second data:
=> Mon, 24 Dec 2012 18:45:00 EET +02:00
As you know, normally, 19:50 is bigger than 18:45. But in this scenario, because of Rails takes day info into calculation, 18:45 looks like bigger than 19:50!
So what is the work around for this issue?
Thanks in advence.
I've found an answer to this at Mongoid's issues list:
https://github.com/mongoid/mongoid/issues/1169
That's really weired...
If I understood well, Time field type is totally useless! What is the difference between DateTime and Time then??
You even can not compare hours and minutes ONLY inside a Time field.
And hold your breath for the work around...
Solution is:
Not to use Time field type! Just use String and compare hours and dates via that procedure.
Person.where(:time.gt => "13:00")
If some could explain this weired situation, I would be happy.
Best way to store time of day is as seconds_since_midnight in an Integer field. Then you can just sort normally.
field :time, type: Integer
validates :time, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 86399 }
http://api.rubyonrails.org/classes/DateTime.html#method-i-seconds_since_midnight
http://api.rubyonrails.org/classes/Time.html#method-i-seconds_since_midnight
On my page I'm using the jquery datetimepicker to get a date and time from the user. When the user selects a datetime, the format of the datetime I get is, for example: "Fri, Sep 21, 1:00PM". I do not get the year since also getting the four digit year makes the whole thing too long for the textbox.
When I pass this date (which is Fri, Sep 21, 1:00PM) back to my controller, and use the update_attributes to update the date in the database, the date that gets inserted is, "0000-09-21 13:00:00.000000". The year becomes 0000 since I was missing the year in the date. I want 2012 obviously. Any ideas how I can achieve this? Please note that I don't want to hardcode 2012 but want it to pick up the current year. Thanks.
Try this
1.9.3p194 :012 > require 'date'
=> true
1.9.3p194 :013 > d= DateTime.parse("Fri, Sep 21, 1:00PM")
=> #<DateTime: 2012-09-21T13:00:00+00:00 ((2456192j,46800s,0n),+0s,2299161j)>
you will get current year.
I am getting the following error:
TypeError: Error #1034: Type Coercion failed: cannot convert "2010-01-02 23:28:17 UTC" to Date.
I am using WebORB to transfer data between Rails and Flex. The data coming from Rails is of type: 'ActiveSupport::TimeWithZone'. I am trying to assign it to a Flex 'Date' data type.
What am I missing?
From the documentation: http://www.adobe.com/livedocs/flex/3/langref/Date.html#Date()
If you pass a string to the Date class constructor, the date can be in a variety of formats, but must at least include the month, date, and year. For example, Feb 1 2005 is valid, but Feb 2005 is not. The following list indicates some of the valid formats:
Day Month Date Hours:Minutes:Seconds GMT Year (for instance, "Tue Feb 1 00:00:00 GMT-0800 2005", which matches toString())
I think in Rails, what you need to do is calling strftime to format the date output that's going to be sent to Flex
time_with_zone.strftime("%a %b %d %H:%M:%S %z %Y") # => "Sun Jan 03 20:58:16 +0700 2010"
Thanks for the help but, oddly, that wasn't it. Your help, Sikachu, did put me on the right track. I couldn't simply assign the returned result--I had to feed it into the constructor. So, instead of doing this, which didn't work:
var flexDate:Date = server_result.date;
I did this, which works:
var flexDate:Date = new Date(server_result.date);