JavaScript中的時間處理小結,javascript處理小結

來源:互聯網
上載者:User

JavaScript中的時間處理小結,javascript處理小結

廢話不多說了,主要通過以下七個方面給大家總結了時間處理相關知識。

1.擷取目前時間

function getNowTime() {return new Date();}

2.時間與天數相加

function getTimeAddDays(time, days) {return new Date(time.getTime() + days * 24 * 60 * 60 * 1000);}

3.擷取並格式化日期:年-月-日

function getFormatDate(time) {return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate();}

4.字串轉換為日期,字串格式:2011-11-20

function convertToDate(strings) {return new Date(Date.parse(strings.replace("-", "/")));}

5.擷取並格式化星期

var WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; //星期function getFormatWeek(time) {return WEEKDAYS[time.getDay()];}

6.時間比較

function compareTime(time1, time2) {return time1.getTime() - time2.getTime();}

7.計算兩個日期之間相隔的天數

function getDays(time1, tiem2){var day = 24*60*60*1000;return (time1.getTime() - time2.getTime())/day;}

小編給大家總結了七個方面有關js中時間處理知識,希望對大家有所協助!

您可能感興趣的文章:
  • JS中處理與目前時間間隔的函數代碼
  • js用Date對象處理時間實現思路及代碼
  • javascript中處理時間戳記為日期格式的方法
  • js處理php輸出時間戳記對不上號的解決方案
  • JavaScript Serializer序列化時間處理樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.