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!
Related
I'm trying to get the time until 12:00 AM in the users timezone. How can I do this in rails?
I want to get the time until 12:00 AM and then add that time to the current time in rails to store it in the database because I want to have a field with the GMT time at is the equivalent to 12:00 AM in the users timezone
I'm using this gem:
gem 'time_difference', '~> 0.5.0'
In order to get the time difference between two timestamps
TimeDifference.between(DateTime.now, created_at)
But I'm not sure how to get the time until 12:00 AM in the users timezone.
Thanks
Given a particular timezone, you can use Rails' tools for dealing with timezones to just directly find out when midnight is for a particular timezone. This example supposes you have a time_zone column on your User model with an appropriate value (e.g., America/Chicago, or anything else Rails supports):
midnight_for_user = ActiveSupport::TimeZone[user.time_zone].now.midnight.tomorrow
For example, I can use the same logic to find when midnight is for a person in New York:
pry(main)> Time.current
=> Thu, 05 Jan 2017 10:34:02 CST -06:00
pry(main)> ActiveSupport::TimeZone['America/New_York'].now.midnight.tomorrow
=> Fri, 06 Jan 2017 00:00:00 EST -05:00
Note that I'm looking for midnight tomorrow; remember that "midnight" for a given day is actually the very first minute of the day, not the last.
I am having a datetime column to store employee check-in ,out times. my logic in the controller action to capture check-in is,
def check_in
#employee = current_user.employee
punch_record = #employee.in_outs.where("date = ?", Date.today).first
punch_record.check_in = DateTime.now
punch_record.save
end
when a person clicks check-in then the above logic gets executed.And in database it saves the check-in by deducting the indian offset ,that is (05:30). and in view page, i am using the in_time_zone method to show the check_in time, back in indian timezone,so that its adding the offset(05:30). and the time is showing properly. this is working fine.
record.check_in.in_time_zone(record.time_zone).strftime(" %I:%M %P")
here the record time_zone = Mumbai
now i am facing the time_zone issue in import attendance, where a person fills the employee check-in,out times in spreadsheet and uploads it. the format that is used in excel sheet is 09:30 am.
so here i am using the following logic to convert it into datetime.
irb(main):004:0> "09:30 am".to_datetime
=> Mon, 07 Nov 2016 09:30:00 +0000
here in this case the offset(05:30) is not getting deducted. how to make the offset deducted in this case?
You aren't comparing like with like.
In the first case you are doing:
record.check_in.in_time_zone(record.time_zone)
Which is taking the recorded datetime object (9:30 am) and converting it to the relevant time zone.
In the second case:
"09:30 am".to_datetime
Which is just setting date time to 9:30 am - you are doing nothing to convert it.
Replicate your logic in rails console by doing:
"09:30 am".to_datetime.in_time_zone('Kolkata')
and you will find you get the right result.
If "9:30 am" is input that isn't UTC but represents the time in India itself, then to convert it to UTC simply do:
"9:30".in_time_zone('Kolkata').utc
=> 2016-11-07 04:00:00 UTC
Would it be possible to go to a certain month of the year with just a given integer. For example
date = Date.today
=> Wed, 30 Dec 2015
What if I want to go back to a certain month based on that date and I am just given a number let's say 7 which is July in the Date::MONTHNAMES so would it be possible to do something like
date = Date.today
=> Wed, 30 Dec 2015
date.go_to_month_of(7) # which will bring me back to July 30, 2015
Okay I found it. It's:
date = Date.today
date.change(:month => x)
Hope this helps you!
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?
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.