SQL Server Float Dates in Ruby

Yesterday, I ran into a situation doing integration with a security app of having to calculate a date to float for SQL Server. Dates as float in SQL Server are calculated from 01/01/1900 00:00:00 so here is the code I used:


  @date = (Time.now.to_f.ceil/86400) + 24105

Time.now.to_f.ceil will return the number of seconds at the end of the day since Epoch Time 01/01/1970. Diving by 8400 will convert that to days and adding 24105 will add the number of days since 01/01/1900.

So to get the time for SQL Server as a float for 30 days from now this is the exact code I used:


  @end_date = ((Time.now + 30.days).to_f.ceil / 86400) + 24105


There are 2 Comments for SQL Server Float Dates in Ruby

I just ran into this today, nearly the exact same situation: SQL Server for a security app from a Rails application. Thanks for putting this together! It helped a lot. The only difference is, the script that I am dealing with is using (Time.now.to_f.floor / 86400) + 25569 Sort of interesting. Still trying to figure out why it want that route instead of the way you mentioned.
By the way, I'm assuming that the "8400" is a typo, meant to read 86400? That confused me thoroughly until I realized that.

Add A Comment

Name:
Email:
URL:
Message:


Powered by MosaicGlobe.