前端過去當前日期 js獲取系統當前時間

2021-10-16 15:53:03 字數 1249 閱讀 4392

一、簡潔版

1.var odate = new date(); //例項乙個時間物件;

odate.getfullyear();   //獲取系統的年;

odate.getmonth()+1;   //獲取系統月份,由於月份是從0開始計算,所以要加1

odate.getdate(); // 獲取系統日,

odate.gethours(); //獲取系統時,

odate.getminutes(); //分

odate.getseconds(); //秒

2.效果圖:

二、動態顯示時間

1、function showtime() {

var now = new date();

var nowtime = now.tolocalestring();

var date = nowtime.substring(0,10);//擷取日期

var time = nowtime.substring(10,20); //擷取時間

var week = now.getday(); //星期

var hour = now.gethours(); //小時

//判斷星期幾

var weeks = ["日","一","二","三","四","五","六"];

var getweek = "星期" + weeks[week];

var sc;

//判斷是am or pm

if(hour >= 0 && hour 

sc = '凌晨';

else if(hour > 5 && hour <= 7){

sc = '早上';

else if(hour > 7 && hour <= 11){

sc = '上午';

else if(hour > 11 && hour <= 13){

sc = '中午';

else if(hour> 13 && hour <= 18){

sc = '下午';

else if(hour > 18 && hour <= 23){

sc = '晚上';

2.3.

setinterval("document.getelementbyid('linkweb').innerhtml=new date().tolocalestring()+' 星期

'+'日一二三四五六'.charat(new date().getday());",1000);

效果:

前端過去當前日期 js如何獲取當前時間並顯示

js可以通過date物件獲取當前日期和時間,使用date 獲取系統當前時間,在使用getfullyear getmonth getdate gethours 等方法獲取特定格式的時間,在使用innerhtm方法顯示。web前端學習 打造全網web前端全棧資料庫 總目錄 看完學的更快,掌握的更加牢固,...

js獲取當前日期

1.date 物件用於處理日期和時間。建立 date 物件的語法 var mydate new date date 物件會自動把當前日期和時間儲存為其初始值。2.引數形式有以下5種 new date month dd,yyyy hh mm ss new date month dd,yyyy new ...

Js獲取當前日期時間

var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getdate 獲取當前日 1 31 mydate.get...