簡體   English   中英

從JavaScript將Clock轉換為Ruby on Rails

[英]Convert Clock to Ruby on Rails from JavaScript

我在當前網站上使用此腳本。 我能夠使其在Ruby on Rails 3中工作,但是我想知道它是否可以在Ruby中重寫。 如果是這樣,我在哪里可以找到有關信息?

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;
}

我建議看Ruby的Time類。 具體來說,就是Time.now()函數。

http://www.ruby-doc.org/core/classes/Time.html

然后查看time.strftime()以確定如何格式化其格式,方法與上述代碼相同。

例:

t = Time.now  
puts t.strftime("%d/%m/%Y %H:%M:%S")  

當然可以,但是需要特別注意的是,正如Steve Wilhelm指出的那樣,您隨后將花費服務器時間,但最重要的是,您將使服務器執行客戶端可以並且應該執行的工作。 重要的是,將盡可能多的瑣碎處理工作分流到客戶端上,以擁有最高效的服務器。 這看起來似乎是一件小事,但是如果每個頁面上都有幾處小事,那么它們可能會成為一件大事,尤其是在服務器開始大量使用時。

就像徒步穿越阿巴拉契亞小徑的人們如何切斷牙刷的柄一樣。 它只有一盎司或兩盎司,但是如果您將該盎司加到其他所有盎司中,他們就切碎了其他東西而掉下來,則可能總計達英鎊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM