Create a datetime using strings in rails - ruby-on-rails

What I would like to do is to go from "2013", "December", "20" and to create 2013-12-20.
Does someone have an idea ? Thanks !

Rails provide nice converters in part of it's framework
2.1.2 :001 > "20 december 2013".to_date
=> Fri, 20 Dec 2013

For your required format you can use this bit for formatting:
strftime("The date is %y-%m-%d")
This can be called on any time object.

You can do this:
Date.new(2013, 12, 20)
You can read more about Date here

This is an extension to #Marc-Alexandre Bérubé 's answer to get your desired format:
"20 december 2013".to_date.strftime("%Y-%m-%d")
# => "2013-12-20"

Related

SOAP4R SOAPDateTime format based on GMT

App uses SOAP4r for consuming API/SOAP
But SOAP::SOAPTimeFormat is returning
2015-11-15T16:59:521468.7999999999999545-04:00
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date))
Using strftime('%Y-%m-%dT%H:%M:%S') is giving the following
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date.strftime('%Y-%m-%dT%H:%M:%S')))
2015-11-15T16:59:52Z
What App needs is
2015-11-15 16:59:52 -0400
Please advise ...need the format in
yyyy-mm-ddThh:mm:ss-/+gmt
-Fransis
A simple change in your strftime and you can find out more in the doc for Time#strftime
basket.purchase_Date.now.strftime('%Y-%m-%d %H:%M %z')
=> "2016-04-26 22:48 -0400"
Seems like your applicaton accepts the iso8601 format. You can use Time#xmlschema as a shortcut to generate iso8601 compatible strings:
basket.purchase_Date.xmlschema
#=> "2015-11-15T16:59:52-04:00"
Just change this line in your example:
chkout.add('purchasedDt ', SOAP::SOAPDateTime.new(basket.purchase_Date.xmlschema))

Ruby On Rails : local_time gem : I dont want it to wrap date with <time> tag

I have been using local_time to convert servertime to client local time. However now I am using datatables and to sort date/time columns. I have to use date-moment.js plugin of Datatables which uses moment.js to handle date conversions.
My problem is: the local_time's view helper is wrapping the date with < time > tag like this
<time data-format="%B %e, %Y %l:%M%P"
data-local="time"
datetime="2013-11-27T23:43:22Z"
title="November 27, 2013 6:43pm EDT"
data-localized="true">November 27, 2013 6:43pm</time>
The wrapping thing is creating problem for moment.js to get the actual date-time as it expects. I need it to convert the date-time but not wrap with <time> tag. Is it possible. If yes How?
currently created a helper method to extract the core date from the string returned by the gem's local_date method
in views/
<%= extract_date(local_date(workflow.created_at, CommonConstants::DATE_FORMAT_LONG)) %>
# Parse the string generated by local_time gem
# Expectation :
# "<time data-format=\"%B %e, %Y\" data-local=\"time\" datetime=\"2015-10-28T11:19:54Z\">October 28, 2015</time>"
def extract_date(date_string)
date_string.split('>').pop.split('</')[0] rescue ''
end

how to enable time series data in flot chart?

Flot documentation says that it is based on javascript timestamp.
I have tried to convert my JSON to comply this standard, and my JSON looks like this
{"label":"Activation","data": [
["1382061194000","10"],["1382061195000","9"],["1382061196000","9"],
["1382061197000","20"],["1382061198000","6"],["1382061203000","16"],
["1382061204000","12"],["1382061205000","14"],["1382061206000","22"],
["1382061207000","20"],["1382061208000","10"],["1382061209000","19"],
["1382061210000","13"],["1382061211000","9"],["1382061212000","12"],
["1382061214000","12"],["1382061215000","17"],["1382061216000","6"],
["1382061217000","14"],["1382061218000","22"],["1382061219000","43"],
["1382061220000","34"],["1382061221000","27"],["1382061222000","31"],
["1382061223000","2"],["1382061224000","1"],["1382061225000","86"],
["1382061226000","82"],["1382061227000","4"],["1382061228000","7"],
["1382061229000","6"],["1382061230000","18"],["1382061231000","17"],
["1382061232000","15"],["1382061233000","3"],["1382061234000","14"],
["1382061235000","2"],["1382061236000","8"],["1382061237000","14"],
["1382061238000","9"],["1382061239000","5"]
]}
And i setup flot options like this
xaxis: {tickSize: 1,mode: "time", timeformat: "%H:%M:%S"}
The chart never up :(
Well actually the goal is simple, I just want to show a flot chart with 08:02:30 08:02:31 as the xaxis info. Thanks for any help.
Your problem is that you're passing in your dates as strings.
> new Date("1382061194000")
Invalid Date
> new Date(1382061194000)
Thu Oct 17 2013 21:53:14 GMT-0400 (EDT)
Try removing the quotes (around your data points too) and see what that does.

Parse a date in rails

I have a date (Which is actually parsed from a PDF) and it could be any of the following format:
MM/DD/YYYY
MM/DD/YY
M/D/YY
October 15, 2007
Oct 15, 2007
Is there any gem or function available in rails or ruby to parse my date?
Or I need to parse it using regex?
BTW I'm using ruby on rails 3.2.
You can try Date.parse(date_string).
You might also use Date#strptime if you need a specific format:
> Date.strptime("10/15/2013", "%m/%d/%Y")
=> Tue, 15 Oct 2013
For a general solution:
format_str = "%m/%d/" + (date_str =~ /\d{4}/ ? "%Y" : "%y")
date = Date.parse(date_str) rescue Date.strptime(date_str, format_str)
I find the chronic gem very easy to use for time parsing and it should work for you. i tried the examples you gave.
https://github.com/mojombo/chronic

rails db won't save string as date with m/d/y format?

saving a string to my db as a date and having some strange results
if the date is formatted like,
dd/mm/yy it will save
if date is formatted like,
mm/dd/yy it will fail to save silently
in my console if i go
'20/10/2012'.to_date
=> Sat, 20 Oct 2012
it works
if i go
'10/20/2012'.to_date
=> ArgumentError: invalid date ...
it breaks
i used an initializer to set up my default date format to %m/%d/%Y which you can see is accurately reflected in my DATE_FORMATS hash.
Date::DATE_FORMATS
=> {:short=>"%e %b", :long=>"%B %e, %Y", :db=>"%Y-%m-%d", :number=>"%Y%m%d", :long_ordinal=>#<Proc:0x007f8663f1aae0#/Users/ian/.rvm/gems/ruby-1.9.3-p0#rails-3.2/gems/activesupport-3.2.1/lib/active_support/core_ext/date/conversions.rb:12 (lambda)>, :rfc822=>"%e %b %Y", :default=>"%m/%d/%Y"}
Uncertain what is the cause of the issue here, as things seem to be configured correctly. How to resolve?
thank you!
Try:
DateTime.strptime('20/10/2012', '%d/%m/%Y')
Or just use Date if you don't need an associated time:
Date.strptime('20/10/2012', '%d/%m/%Y')
Use gem american_date
In gem file
gem 'american_date'
Now it will save mm/dd/yyyy date format.

Resources