Reputation: 3331
I'm using this script in my current website. I'm able to make it work in Ruby on Rails 3, but I was wondering could it be rewritten in Ruby. If so, where can I find information on it?
function init ( )
{
timeDisplay = document.createTextNode ( "" );
document.getElementById("clock").appendChild ( timeDisplay );
}
function updateClock ( )
{
var currentTime = new Date ( );
var currentDay = currentTime.getDay ( );
//Convert the day component to day abbreviation
currentDay = ( currentDay == 0 ) ? "Sun" : currentDay;
currentDay = ( currentDay == 1 ) ? "Mon" : currentDay;
currentDay = ( currentDay == 2 ) ? "Tue" : currentDay;
currentDay = ( currentDay == 3 ) ? "Wed" : currentDay;
currentDay = ( currentDay == 4 ) ? "Thu" : currentDay;
currentDay = ( currentDay == 5 ) ? "Fri" : currentDay;
currentDay = ( currentDay == 6 ) ? "Sat" : currentDay;
var currentMonth = currentTime.getMonth( );
//Convert the month component to text month
currentMonth = ( currentMonth == 0 ) ? "January" : currentMonth;
currentMonth = ( currentMonth == 1 ) ? "February" : currentMonth;
currentMonth = ( currentMonth == 2 ) ? "March" : currentMonth;
currentMonth = ( currentMonth == 3 ) ? "April" : currentMonth;
currentMonth = ( currentMonth == 4 ) ? "May" : currentMonth;
currentMonth = ( currentMonth == 5 ) ? "June" : currentMonth;
currentMonth = ( currentMonth == 6 ) ? "July" : currentMonth;
currentMonth = ( currentMonth == 7 ) ? "August" : currentMonth;
currentMonth = ( currentMonth == 8 ) ? "September" : currentMonth;
currentMonth = ( currentMonth == 9 ) ? "October" : currentMonth;
currentMonth = ( currentMonth == 10) ? "November" : currentMonth;
currentMonth = ( currentMonth == 11) ? "December" : currentMonth;
var currentDate = currentTime.getDate( );
// Add suffix to the date
currentDate = ( currentDate == 1 || currentDate == 21 || currentDate == 31 ) ? currentDate + "st" : currentDate;
currentDate = ( currentDate == 2 || currentDate == 22 ) ? currentDate + "nd" : currentDate;
currentDate = ( currentDate == 3 ) || currentDate == 23 ? currentDate + "rd" : currentDate;
currentDate = ( currentDate > 3 || currentDate < 21 || currentDate > 23 || currentDate < 31 ) ? currentDate + "th" : currentDate;
var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;
// Compose the string for display
var currentTimeString = "Today is : " + currentDay + " " + currentMonth + " " + currentDate + " " + currentHours + ":" + currentMinutes + " " + timeOfDay;
// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
Upvotes: 0
Views: 917
Reputation:
Sure you could do it but it's important to note that, as Steve Wilhelm pointed out, you would then be using the server time but most importantly you would be making the server do work that the client can, and should, be doing. It's important to offload as much trivial processing onto the client as possible to have the most efficient server. It may seem like a minor thing but if you have several minor things on every page they can up to a major thing especially when your server starts getting used a lot.
It's like how people that hike the Appalachian trail cut off the handle of their toothbrush. It's only an ounce or two but if you add that ounce to all the other ounces they shed from chopping up their other stuff it can add up to pounds.
Upvotes: 0
Reputation: 3924
I'd suggest looking at Ruby's Time
class. Specifically, the Time.now()
function.
http://www.ruby-doc.org/core/classes/Time.html
And then look at time.strftime()
to determine how to format it the same as in your above code.
Example:
t = Time.now
puts t.strftime("%d/%m/%Y %H:%M:%S")
Upvotes: 3