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.
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 building a schedule in rails and I need to see if today's date matches the date of an event in the future. I have an event model with a date field and I know the dates of all the future events, but how do I add those dates to the table in a way rails will understand?
I'd like to do something like:
if Time.now is equal to next event
# do this
Time.now outputs
2014-03-18 11:05:14 -0500
How do I add that sort of format to the dates in the table so that I can compare Time.now to the date of the future event?
EDIT
Say I'm creating a new Event in the rails console, And I want the date of the Event to be April 1, 2014. I'm trying to figure out what syntax I need to enter the date in the console. For example, to create an Event in console I would do something like
Event.create(name: "Event1", date: ??)
What do I put in place of the question marks for the date of April 1, 2014 in syntax understood by the computer? I need April 1, 2014 in a datetime format, not a string.
Thank you!
seeds.rb is—as the file extension implies—just Ruby.
You can supply the date in any form that an available Ruby library can parse into a Time or Date object.
If you want to keep it human-readable, you can use Date.parse:
Event.create(name: 'Event1', date: Date.parse('Apr 1 2014'))
Event.create(name: 'Event2', date: Date.parse('April 1, 2014'))
Event.create(name: 'Event3', date: Date.parse('1-Apr-14'))
Or, if you need human-readable times as well, Time.parse:
Event.create(name: 'Event4', date: Time.parse('09:15 1 Apr 2014'))
Event.create(name: 'Event5', date: Time.parse('April 1, 2014, 9:15am'))
Event.create(name: 'Event6', date: Time.parse('1-Apr-14, 09:15 -0700'))
If you prefer to use UNIX timestamps, Time.at creates a new Time object from them (and offers a mild performance benefit over parsing textual dates):
Event.create(name: 'Event7', date: Time.at(1395166977))
If you have defined next_event as a datetime column in database, rails will automatically initialize it as a date_time object when you load it from database. All you need is this
if Time.now.to_date == next_event.to_date
For adding the future days, you can use days.from_now method
For eg
1.days.from_now #Wed, 19 Mar 2014 16:29:26 UTC +00:00
2.days_from_now #Thu, 20 Mar 2014 16:30:16 UTC +00:00
Or you can use normal addition
Time.now + 2.days
Time.now + 2.hours
Time.now + 10.minutes
Event.create(name: "Event1", date: (Time.now + 1.day))
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
I got an input field where the user inputs a date, currently I'm using Chronic and the user likes it.
If the user enters last jan using Chronic and strftime I get January 2013 which is good, the issue comes when:
the user input is an upcoming month like January, the parsing I'm doing returns January 2014 and not January 2013 wich is what the user wants.
result_date = Chronic.parse(params[:date]).strftime('%B %Y')
Is there a way that I can set as default the current year? So when the user puts January it is parsed to January 2013 ?
Thanks!
I was able to figure it out!
result_date = Chronic.parse(params[:date], :context => :past).strftime('%B %Y')
Now if the user input is a single month like Jan the outcome will be January 2013
I hope it helps someone else!