Convert RSS pubDate Date to MM-DD-YY HH:MM - ruby

I have a string, the value of this string is an rss date. I want to convert the value of this string / the rss date into another format (MM-DD-YY HH:MM). Any ideas how to do this?
This is an extract of what I got so far...
#!/usr/bin/env ruby
require 'rss/2.0'
require 'rss/content'
[...]
date = rss.items[i].pubDate
date = #Here I want to convert the date
puts date
[...]

Use Date.parse to get a Date object from a string and then Date#strftime to format it correctly.
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-parse
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
Here's a strftime example:
Time.now.strftime("%m-%d-%Y %H:%M") #=> "03-27-2012 12:03

Related

String to Date Format as DD/MM/YYY in Python - Portuguese

I would like to convert the string "23/02/2018" to a date format as 23-fev-2018.
Most importanly is that, the month must be in portuguese language, refering to fevereiro.
My issue is that usually the datetime.date prints like (YYYY,MM,DD):
import datetime
datestr = "23/02/2018"
dateobj = datetime.datetime.strptime(datestr, "%d/%m/%Y")
print dateobj #year, month, day
How may I print from a string as 23/10/2017 to date format as 23-out-2017, refering to the month "outubro" in portuguese?
Use the locale module.
import locale
import datetime
locale.setlocale(locale.LC_ALL, 'pt_pt.UTF-8')
datetime.datetime.strptime('23/10/2017', '%d/%m/%Y').strftime('%d/%B/%Y')
# '23/Outubro/2017'
datetime.datetime.strptime('23/10/2017', '%d/%m/%Y').strftime('%d/%b/%Y')
# '23/Out/2017'

Changing the format from a mySQL datetime format to a different type in ruby?

I am attempting to change an SQL datetime variable (2016-06-09 14:29:34) into a format that looks like this (00:00_20160601). I have tried to follow a couple of SO questions that will allow me to format a Time object.
This is what I have done so far:
start_datetime = "2016-06-09 14:29:34"
t =Time.new(start_datetime)
t.strftime("%H:%M_%Y%d%m")
This results in the time being formatted to 2016-01-01 00:00:00 +0000, which is obviously not what I want. I was wondering if someone could help me format the datetime object the way I specified?
You can do this with DateTime:
require 'datetime'
DateTime.parse("2016-06-09 14:29:34").strftime("%H:%M_%Y%d%m")
#=> "14:29_20160906"
The format you're feeding in is basically ISO-8601 so it's parsed by default.
Feeding that value into Time.new is completely incorrect. The first argument there is the year, the rest have to be supplied separately. That's why you get 2016-01-01, since everything else comes out as defaults.
Time.new is converting automatically and the result of "2016-06-09 14:29:34".to_i is 2016.
It's not entirely clear why your day value changes from 09 in the input to 01 in the desired output, so I'll use the normal thing and output the same as was input:
require 'time'
start_datetime = "2016-06-09 14:29:34"
t = Time.strptime(start_datetime, '%Y-%m-%d %H:%M:%S')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"
Since the hours and minutes are being thrown away there are a couple of other ways to go about this.
Ignore the hours and minutes when parsing:
t = Time.strptime(start_datetime, '%Y-%m-%d')
Or use a Date object instead of a Time object:
require 'date'
start_datetime = "2016-06-09 14:29:34"
t = Date.strptime(start_datetime, '%Y-%m-%d')
t.strftime('00:00_%Y%m%d') # => "00:00_20160609"

What kind of date format is this and how do I transform it?

Making a GET request to a private (no public documentation) API returns data in JSON format.
The value for date looks as follows:
AanmeldDatum: "/Date(1262300400000+0100)/"
There's another variable called AangebodenSindsTekst which means OfferedSinceText and it's value is "8 augustus 2014". So the unknown Date format should get parsed into that specific value.
I'm wondering what kind of date format it is and how can I transform this to something like this 2014-08-08 with Ruby?
I've tried this:
require 'time'
t = '1262300400000+0100'
t2 = Time.parse(t)
# => ArgumentError: no time information in "1262300400000+0100"
Ruby's Time class is your friend, especially the strptime method:
require 'time'
foo = Time.strptime('1262300400000+0100', '%N') # => 2014-08-08 16:57:25 -0700
foo = Time.strptime('1262300400000+0100', '%N%z') # => 2014-08-08 08:57:25 -0700
%N tells Ruby to use nanoseconds. It's throwing away the precision after the 9th digit which is OK if you don't need the rest of the value. Nanosecond accuracy is good enough for most of us.
%z tells Ruby to find the timezone offset, which it then applies to the returned value.
While parse can often figure out how to tear apart an incoming string, it's not bullet-proof, nor is it all-knowing. For speed, I'd recommend learning and relying on strptime if your strings are consistent.
As the Tin Man pointed out in this answer, use the following instead:
Time.strptime('1262300400000+0100', '%Q%z')
it could be milliseconds since epoc, take off the last 3 zeros and plug it into a unix time stamp converter, comes out as Dec 31st 2009
TIME STAMP: 1262300400
DATE (M/D/Y # h:m:s): 12 / 31 / 09 # 11:00:00pm UTC

Remove the date from a string

Looking for some Regex help in Ruby.
I have a string that is formated as so:
YYYY-MM-DD-title-of-post.markdown
I'm looking to use gsub or sub to remove the YYYY-MM-DD portion of the string and do further processing to it.
I would like a Regular Expression to remove the date from the string. The date will never be in a different format.
Thanks for the help!
This should do the trick:
new_title = old_title.gsub('\d{4}-\d{2}-\d{2}','')
If you are confident that the date will ALWAYS be in the format YYYY-MM-DD and will ALWAYS appear at the beginning of the string than the following regex will work:
my_string = "YYYY-MM-DD-title-of-post.markdown"
date = my_string.match(/^(\d{4}-\d{2}-\d{2})-.*/)[1]

How do I format a date to mm/dd/yyyy in Ruby?

In Perl you can do:
my $current_time = DateTime->now();
my $mdy = $current_time->mdy("/");
What's the easiest way to do this in Ruby?
The strftime method can be used to format times:
Time.now.strftime("%m/%d/%Y")
I wrote a gem to help with formatting dates, and keeping your views DRY (not having to strftime every time you want to format dates).
Check it out at: http://github.com/platform45/easy_dates
You can simply use %D with strftime method
> Time.now.strftime("%D")
=> "11/24/14" # mm/dd/yy
my $current_time = DateTime->now();
my_current_time = DateTime.now
my $mdy = $current_time->mdy("/");
my_mdy = my_current_time.strftime("%m/%d/%Y")

Resources