Convert facebook date into timestamp format - ruby-on-rails

I get birthday from facebook in this format
"12/09/1993"
But in timestamp format is yyyy/mm/dd How I convert facebook data into this with min lines of code?

Here you can see different sort of combinations:
require 'date'
date = "01/07/2016 09:17AM"
DateTime.parse(date).strftime("%A, %b %d")
#=> Friday, Jul 01
DateTime.parse(date).strftime("%m/%d/%Y")
#=> 07/01/2016
DateTime.parse(date).strftime("%m-%e-%y %H:%M")
#=> 07- 1-16 09:17
DateTime.parse(date).strftime("%b %e")
#=> Jul 1
DateTime.parse(date).strftime("%l:%M %p")
#=> 9:17 AM
DateTime.parse(date).strftime("%B %Y")
#=> July 2016
DateTime.parse(date).strftime("%b %d, %Y")
#=> Jul 01, 2016
DateTime.parse(date).strftime("%a, %e %b %Y %H:%M:%S %z")
#=> Fri, 1 Jul 2016 09:17:00 +0200
DateTime.parse(date).strftime("%Y-%m-%dT%l:%M:%S%z")
#=> 2016-07-01T 9:17:00+0200
DateTime.parse(date).strftime("%I:%M:%S %p")
#=> 09:17:00 AM
DateTime.parse(date).strftime("%H:%M:%S")
#=> 09:17:00
DateTime.parse(date).strftime("%e %b %Y %H:%M:%S%p")
#=> 1 Jul 2016 09:17:00AM
DateTime.parse(date).strftime("%d.%m.%y")
#=> 01.07.16
DateTime.parse(date).strftime("%A, %d %b %Y %l:%M %p")
#=> Friday, 01 Jul 2016 9:17 AM
Even more combinations here: http://www.foragoodstrftime.com/

Time.parse("12/09/1993").strftime("%Y/%m/%d")
#=> "1993/09/12"

Use strftime
date = "12/09/1993".to_date # => Sun, 12 Sep 1993
date.strftime("%Y/%m/%d") # => "1993/09/12"

Related

Add 30 seonds to minutes in time class in Ruby

I used the following format in Ruby which is working fine.
Time.new.strftime("%a %d %b, %Y %H:%M") # output "Thu 05 Aug, 2021 12:29"
But I need to add additional 30 seconds to the current time. I tried with active support, but unable to figure out how to do it. Any help would be appreciated.
Given an instance of time: (Rails flavored)
t = Time.current
#=> Thu, 05 Aug 2021 10:55:12 CEST +02:00
There are several ways to add 30 seconds to it, e.g.
Ruby's Time#+ with an integer:
t + 30
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
Ruby's Time#+ with a Rails' ActiveSupport::Duration instance from Numeric#seconds:
t + 30.seconds
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
Rails' Time#advance which takes a hash:
t.advance(seconds: 30)
#=> Thu, 05 Aug 2021 10:55:42 CEST +02:00
%S - Second of the minute (00..59)
Time.new.strftime("%a %d %b, %Y %H:%M:%S")
# "Thu 05 Aug, 2021 10:18:50"
+ 30 seconds
(Time.new + 30.seconds).strftime("%a %d %b, %Y %H:%M:%S")
Or 0.5 min (30 sec)
(Time.new + 0.5.minutes).strftime("%a %d %b, %Y %H:%M:%S")
There is a method since for time object which can be used for this purpose
Time.new.since(30.seconds).strftime("%a %d %b, %Y %H:%M:%S")

Ruby: get last day of month while sum

Looking for a best way to get month with last day during adding operation.
For examle: I am getting last day of Movember and I need to add 1 month from this day, 1 full calendar month:
2.6.3 :002 > date = Date.strptime('November 30, 2020', '%B %d, %Y')
=> Mon, 30 Nov 2020
2.6.3 :003 > date + 1.month
=> Wed, 30 Dec 2020
This is wrong for me, because it will be 31 days in December.
Another example:
2.6.3 :005 > date2 = Date.strptime('February 29, 2020', '%B %d, %Y')
=> Sat, 29 Feb 2020
2.6.3 :006 > date2 + 1.month
=> Sun, 29 Mar 2020
I am expecting 31 of Mar instead of 29
For now - I know only one solution - something like:
2.6.3 :017 > (date + 1.month).end_of_month
=> Thu, 31 Dec 2020
2.6.3 :018 > (date2 + 1.month).end_of_month
=> Tue, 31 Mar 2020
Is it the correct and best way?
I believe the solution you came up with is the right way to solve the problem. What I had in mind is very similar:
date.next_month.end_of_month
It's easy to do this with the pure-Ruby methods Date::new and Date#>>.
require 'date'
def end_of_n_months_hence(curr_date, n)
(Date.new(curr_date.year, curr_date.month, 1) >> (n+1)) - 1
end
curr_date = Date.today
#=> #<Date: 2020-11-25 ((2459179j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, 0)
#=> #<Date: 2020-11-30 ((2459184j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, 1)
#=> #<Date: 2020-12-31 ((2459215j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, 2)
#=> #<Date: 2021-01-31 ((2459246j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, -1)
#=> #<Date: 2020-10-31 ((2459154j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, -2)
#=> #<Date: 2020-09-30 ((2459123j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, 12)
#=> #<Date: 2021-11-30 ((2459549j,0s,0n),+0s,2299161j)>
end_of_n_months_hence(curr_date, 13)
#=> #<Date: 2021-12-31 ((2459580j,0s,0n),+0s,2299161j)>
I don't know Rails but I expect you could change the operative line of the method to:
(curr_date >> n).end_of_month

Rails not Date.strptime not getting accurate op

07/28/2017 11:56 PM This is my date and below is my current code:
Date.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p').to_time
i'm getting o/p => 2017-07-28 00:00:00 +0530 but i want hours.What exactly should i do ?
The problem is that you're converting it into a Date object which doesn't have any time component and then converting it back into a Time object. Instead, you can use either
Time.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p')
# => 2017-07-28 23:56:00 -0700
or
DateTime.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p')
# => Fri, 28 Jul 2017 23:56:00 +0000
and you call call to_time on the DateTime if you'd like
DateTime.strptime("07/28/2017 11:56 PM", '%m/%d/%Y %H:%M %p').to_time
# => 2017-07-28 23:56:00 +0000

Rails 5: Not converting DateTime object timezone correctly

This issue has been eating away at me for a while.
I take a date time string:
[14] pry(#<EventsController>)> params[:event][:ended_at]
=> "05/31/2017 2:00 PM"
I convert it to a DateTime object:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
If I run the in_time_zone method on the to_datetime object it outputs the wrong time for pacific timezone:
[16] pry(#<EventsController>)> to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 07:00:00 PDT -07:00
It should read "2:00PM" the same as how it is entered.
If I go to google and check to see if "14:00:00 +0000" is the correct entry for PDT time it verifies as correct:
http://imgur.com/a/ZJ80F
Any clues about why it's not converting correctly?
the error comes that the ended_at is been assumed by the system configuration that in this case is '+0000' you need to include the original timezone the ended_at is.
irb(main):001:0> date = "05/31/2017 2:00 PM"
=> "05/31/2017 2:00 PM"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
Note that this one has already set as UTC since was assumed by the system the timezone
irb(main):001:0> date = "05/31/2017 2:00 PM -0700"
=> "05/31/2017 2:00 PM -0700"
irb(main):002:0> to_datetime = DateTime.strptime(date, "%m/%d/%Y %H:%M %p %z")
=> Wed, 31 May 2017 14:00:00 -0700
irb(main):003:0> new_to_datetime = to_datetime.utc
=> Wed, 31 May 2017 21:00:00 +0000
irb(main):004:0> new_to_datetime.in_time_zone("Pacific Time (US & Canada)")
=> Wed, 31 May 2017 14:00:00 PDT -07:00
Update
#antonio's comment mention he was 1 hour off
irb(main):046:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z").class
=> DateTime
irb(main):047:0> time = DateTime.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> Wed, 31 May 2017 14:00:00 -0800
irb(main):048:0> time.utc.class
=> Time
As you can see these are different classes and that's sign of problems what you can do is use Time instead of DateTime
irb(main):049:0> time = Time.strptime(date + " Pacific Time (US & Canada)", "%m/%d/%Y %H:%M %p %Z")
=> 2017-05-31 14:00:00 -0700
irb(main):050:0> time.class
=> Time
If we look at your first attempt:
pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p")
=> Wed, 31 May 2017 14:00:00 +0000
we see that "05/31/2017 2:00 PM" is being parsed correctly in UTC, hence the +0000 offset.
Then, if you convert that UTC time to PDT, you apply a -7 hour adjustment:
=> Wed, 31 May 2017 07:00:00 PDT -07:00
# -------------------------------^^^^^^
and the 14:00:00 becomes 07:00:00 because 14 - 7 == 7.
If you want to_datetime to be in PDT then it would be easiest to start that way by telling DateTime.strptime that the timestamp string is PDT:
no_tz = '05/31/2017 2:00 PM'
to_datetime = DateTime.strptime("#{no_tz} PDT", '%m/%d/%Y %H:%M %p %z')
# ------------------------------^^^^^^^^^^^^^^^---------------------^^
Then you'll get the 14:00:00 PDT you're looking for:
> to_datetime.iso8601
=> "2017-05-31T14:00:00-07:00"
# -------------^^------^^^^^^

Time conversion in ruby

I have data as following
pubdate=10 hours ago +0000
pudate=Yesterday +0000
pubdate=1 min ago +0000
i want to convert in this format like this
Wed Jan 30 10:00:00 +0000 2013
please someone help me
You can use strftime on date, datetime object to convert into whatever format you want
1.9.3p327 :006 > date = Date.today
 => Wed, 30 Jan 2013 
1.9.3p327 :007 > time = Time.now
 => 2013-01-30 17:00:30 +0530 
1.9.3p327 :008 > date.strftime("%a %b %d %H:%M:%S %z %Y")
 => "Wed Jan 30 00:00:00 +0000 2013" 
1.9.3p327 :009 > time.strftime("%a %b %d %H:%M:%S %z %Y")
 => "Wed Jan 30 17:00:30 +0530 2013" 

Resources