//js date對象常用方法

來源:互聯網
上載者:User

標籤:超過   2017年   min   set   eth   返回   gets   js date   例子   

//js date對象常用方法
//建立事件對象
var now = new Date(); //不傳參情況下預設返回目前時間
//擷取年
var y=now.getFullYear();
//擷取月
var m=now.getMonth()+1;//返回0-11
//擷取日
var d= now.getDate();
//擷取小時
var h =now.getHours();
//擷取分鐘
var i =now.getMinutes();
//擷取秒
var s =now.getSeconds();
//擷取星期幾
var week =now.getDay();//0-6 0為周日
var weeks=[‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘];
console.log(y+"年"+m+‘月‘+d+‘日‘+h+‘時‘+i+‘分‘+s+‘秒‘+‘星期‘+weeks[week]);//2017年7月17日17時55分41秒星期一
var times =now.getTime();//擷取時間戳記
//以上 所有方法get 換成set 例 setFullYear() setMonth() 是設定時間對象
//如果設定 月 傳參 setMonth(13) 超過月份數 則 年份加1 以此類推

//例子:計算50天后星期幾
//方法1:
now.setDate(now.getDate()+50);
console.log(weeks[now.getDay()]);
//方法二
var now = new Date();
var mytime=new Date(now.getFullYear(),now.getMonth(),now.getDate()+50);//依次設定年月日 時分秒 年月日必須 時分秒可選
console.log(weeks[mytime.getDay()])

//js date對象常用方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.