Ruby DateTime to Javascript Date Conversion

When using a Ruby / Rails based API, you often need to deal with dates in a coherent manner. Say I have a model called Post which looks like the following:

def self.up
create_table :posts do |t|
t.string :title
t.text :message
t.timestamps
end
end

If you’ve been coding in Rails for any length of time, you know that t.timestamps simply creates our created_at and updated_at fields on our model, which are handled without our intervention 99% of the time. But what happens when you have an AJAX-ified user interface, or your passing data to a mobile application, or your using a Node.js application to parse the data and publish it somewhere? Ruby dates and JS Dates and theres no way to directly parse between them.

A javascript date can look like this:

var d = new Date();
>>returns Thu Sep 01 2011 09:47:01 GMT-0400 (EDT)

var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

But a Ruby date looks like this:

d = new DateTime()
>> returns Mon, 01 Jan -4712 00:00:00 +0000

Now one way of doing this is to use a d.strftime() call to format it in a Javascript parse-able manner. Much quicker though, is to use the numerical version of the number.

Ruby:

@post = Post.first
"returns first post object"
@post.created_at
>> returns Thu, 01 Sep 2011 09:50:12 -0400
@post.created_at.to_i
>> returns 1314885012
@post["created_at_in_secs"] = @post.created_at.to_i

Now I pass back in my params to the AJAX call (usually as JSON) and can quickly parse it in Javascript

JS:

var postSeconds = < %= @post["created_at_in_secs"] %>
var d = new Date(Number(postSeconds) * 1000);
>> returns Thu Sep 01 2011 09:50:12 GMT-0400 (EDT)


Using Mongrel with Ruby 1.9.2

**  Update ** for the time being, I’m disabling all of the commenting functionality on my posts because of terrible amounts of spam circumventing my captcha.  If you need to contact me for a question in particular, please reach me at kevin at h-pk-ns.com.  Thanks!

 

So I’ve been working recently on a Rails application and am using Ruby 1.9.2 and Rails 3.0.7 for development. For some reason, not sure why, I just really don’t like WEBrick. It just seems slower overall so I greatly prefer using Mongrel or Thin.

Trying to install mongrel though, gave me a bit of a pain at first.
gem install mongrel simply just does not work anymore. It spits out:

Building native extensions. This could take a while...
ERROR: Error installing mongrel:
ERROR: Failed to build gem native extension.
Continue reading


Date Selector in Titanium – Creating an iCal Look-Alike

**  Update ** for the time being, I’m disabling all of the commenting functionality on my posts because of terrible amounts of spam circumventing my captcha.  If you need to contact me for a question in particular, please reach me at kevin at h-pk-ns.com.  Thanks!

 

Source Code: https://github.com/khopkins218/DateSelectorEx

One of the more complex UI elements when dealing with any mobile device, is how to approach-ably handle selecting dates and times for events, whether it be meetings, alarm clocks, to-do lists, et cetera. Rather than re-invent the wheel, look to those who have done it RIGHT the first time!
Continue reading


Image Views in Titanium – How to Deal with Caching

**  Update ** for the time being, I’m disabling all of the commenting functionality on my posts because of terrible amounts of spam circumventing my captcha.  If you need to contact me for a question in particular, please reach me at kevin at h-pk-ns.com.  Thanks!

 

So lately I’ve been doing a lot of client work on the mobile side of things, which has been an all new adventure from the start.  Things are much much different in the mobile world.  You have to think a bit differently and solve problems in a way that you almost feel is unclean.

Titanium has been great, I’ve had two apps done and successfully pass review to the App Store and Android Market in about the past two months.  During the first one, a conference specific app, there were a lot of remote images being pulled from a server running Expression Engine.  Basically, the client was entering the data and uploading the photos into EE, which I then used to generate a real-time XML feed that I pumped into the mobile app.

Works great right?  Yeah, until you run into dealing with image URLs. Continue reading


Welcome

No, this isn’t a default generated post by WordPress, this is me. Continue reading


SEO Powered by Platinum SEO from Techblissonline

Kevin's Blog is Stephen Fry proof thanks to caching by WP Super Cache