javascript獲取當前電腦系統時間程式碼例項

antzone發表於2017-04-07

在以前很多網站,在網頁的某一部分會顯示一個時間日期。

可能初學者或者沒有相關專業知識的人感覺很高階,其實絕大多數就是獲取當前電腦得系統時間。

下面就是一段能夠實現此功能的例項程式碼:

[JavaScript] 純文字檢視 複製程式碼
var curr_time = new Date();
var strDate = curr_time.getYear()+"年";
strDate += curr_time.getMonth()+1+"月";
strDate += curr_time.getDate()+"日";
strDate += curr_time.getHours()+":";
strDate += curr_time.getMinutes()+":";
strDate += curr_time.getSeconds();
console.log(strDate);
  
var curr_time = new Date();
with(curr_time){
  //定義變數,併為其賦值為當前年份,後加中文“年”字標識
  var strDate = getYear()+1900+"年";
  //取當前月份。注意月份從0開始,所以需加1,後加中文“月”字標識
  strDate +=getMonth()+1+"月";
  strDate +=getDate()+"日"; //取當前日期,後加中文“日”字標識
  strDate +=getHours()+":"; //取當前小時
  strDate +=getMinutes()+":"; //取當前分鐘
  strDate +=getSeconds(); //取當前秒數
  console.log(strDate); //結果輸出
}

相關文章