javascript-封裝Date日期類

來源:互聯網
上載者:User

標籤:問題   var   截取   日期   定義   ini   tostring   param   方法   

遷移時間:2017年5月27日18:43:02Author:Marydonjavascript-封裝Date日期類  (一)對日期進行格式化
//自訂Date日期類的format()格式化方法<script type="text/javascript">    // 對Date的擴充,將 Date 轉化為指定格式的String    // 月(M)、日(d)、小時(H)、分(m)、秒(s)、季度(q) 可以用 1-2 個預留位置,     // 年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字)     // 例子:     // (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2016-09-19 16:32:53.731    // (new Date()).Format("yyyy-M-d H:m:s:S")      ==> 2016-9-19 16:40:9:955     Date.prototype.Format = function (fmt) { //author: meizz         var o = {            "M+": this.getMonth() + 1, //月份             "d+": this.getDate(), //日             "H+": this.getHours(), //小時             "m+": this.getMinutes(), //分             "s+": this.getSeconds(), //秒             "q+": Math.floor((this.getMonth() + 3) / 3), //季度             "S": this.getMilliseconds() //毫秒         };        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));        for (var k in o)        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));        return fmt;    };    window.onload=function() {        var date = new Date();        var b = date.toLocaleDateString();//擷取的格式為:2016年9月19日        var d = date.toLocaleTimeString();//下午4:42:46        var e = date.toLocaleString();//2016年9月19日 下午4:44:02        var f = date.toDateString();//Mon Sep 19 2016        var g = date.toUTCString();//Mon, 19 Sep 2016 08:45:42 GMT        var h = date.toString();//Mon Sep 19 2016 16:46:23 GMT+0800 (中國標準時間)        //自訂日期格式        var c = date.Format("yyyy-MM-dd HH:mm:ss");//format()方法是自訂的        document.getElementById("aa").value = c;    };</script>

  (二)根據日期返回本周周一和周日的日期

/** * 根據日期返回本周周一和周日的日期 * @param day *     參數日期 * @param num *     第幾周 * @return oneWeek *     周一,周日所在日期     */function getWeekDate(day,num) {        num = num || 0;    // 傳回值:周一和周日所在的日期    var oneWeek = {};    // 初始日期    var initDate = "";    // 截取年月日    initDate = day.split(‘ ‘)[0];    // ie相容性問題,將yyyy-MM-dd轉換成yyyy/MM/dd    initDate = initDate.replace(/-/g,"/");    // 將string轉換成Date    initDate = new Date(Date.parse(initDate));//格式只能是yyyy/MM/dd        // 返回 day距離1970 年 1 月 1 日0時0分的毫秒數    var nowTime = initDate.getTime();     // 返回星期的某一天的數字: 0(周日) 到 6(周六)    var weekNum = initDate.getDay();    // 一天所代表的毫秒數    var oneDayTime = 24 * 60 * 60 * 1000;         //顯示周一    var MondayTime = nowTime - (weekNum - 1) * oneDayTime ;     //顯示周日    var SundayTime =  nowTime + (7 - weekNum) * oneDayTime ;     if (0 != num) {        MondayTime += 7 * num * oneDayTime;        SundayTime += 7 * num * oneDayTime;    }        //初始化日期時間    var monday = new Date(MondayTime);    var sunday = new Date(SundayTime);    // formatDate是自訂的格式化方法    monday = monday.formatDate(‘yyyy-MM-dd‘);    sunday = sunday.formatDate(‘yyyy-MM-dd‘);        oneWeek.Monday = monday;    oneWeek.Sunday = sunday;        return oneWeek;}

    測試:

var week = getWeekDate(‘2017-05-27‘,0);        console.log(week.Monday + "," + week.Sunday);

 

 

javascript-封裝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.