代碼收藏系列--javascript--日期函數

來源:互聯網
上載者:User

標籤:eee   string   ref   lin   --   預留位置   ==   函數   星期幾   

/** * 將 Date 轉化為指定格式的String  * 月(M)、日(d)、12小時(h)、24小時(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 個預留位置  * 年(y)可以用 1-4 個預留位置,毫秒(S)只能用 1 個預留位置(是 1-3 位的數字) * @param Date date * @param string fmt * @returns string */function formatDate(date, fmt) { //author: meizz     var o = {        "M+": date.getMonth() + 1, //月份                 "d+": date.getDate(), //日                 "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小時                 "H+": date.getHours(), //小時                 "m+": date.getMinutes(), //分                 "s+": date.getSeconds(), //秒                 "q+": Math.floor((date.getMonth() + 3) / 3), //季度                 "S": date.getMilliseconds() //毫秒             };    var week = ["天", "一", "二", "三", "四", "五", "六"];    if (/(y+)/.test(fmt)) {        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));    }    if (/(E+)/.test(fmt)) {        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[date.getDay() + ""]);    }    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;}// 樣本console.log(formatDate(new Date(), "yyyy-MM-dd hh:mm:ss.S")); // 2017-07-11 13:07:12console.log(formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")); // 2017-07-11 13:07:12console.log(formatDate(new Date(), "yyyy-M-d h:m:s.S")); // 17-7-11 13:7:12console.log(formatDate(new Date(), "yy-M-d H:m:s")); // 17-7-11 13:7:12console.log(formatDate(new Date(), "yyyy-MM-dd E")); // 2017-07-11 13:07:12console.log(formatDate(new Date(), "yyyy-MM-dd EE")); // 2017-07-11 13:07:12console.log(formatDate(new Date(), "yyyy-MM-dd EEE")); // 2017-07-11 13:07:12/** * 判斷指定日期是一年中的第幾周 * @param Date date * @return integer */function getWeekNumber(date) {    var totalDays = 0;    var years = date.getYear()    if (years < 1000) {        years += 1900;    }    var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];    //判斷是否為閏年,針對2月的天數進行計算    if (Math.round(date.getYear() / 4) == date.getYear() / 4) {        days[1] = 29    }    var curMonth = date.getMonth();    totalDays = date.getDate();    for (var i = 0; i < curMonth; i++) {        totalDays += +days[i];    }    //那一年第一天是星期幾    var yearFirstDay = new Date(years, 0, 1).getDay() || 7;    var week = null;    if (yearFirstDay == 1) {        week = Math.ceil(totalDays / yearFirstDay);    } else {        totalDays -= (7 - yearFirstDay + 1);        week = Math.ceil(totalDays / 7) + 1;    }    return week;}// 樣本console.log(getWeekNumber(new Date())); //29console.log(getWeekNumber(new Date(‘2017-01-01‘))); // 1console.log(getWeekNumber(new Date(‘2017-12-31‘))); // 53console.log(getWeekNumber(new Date(‘2000-05-08‘))); // 20

下載:[js_date_function_demo.zip]

 

著作權聲明:本文採用署名-非商業性使用-相同方式共用(CC BY-NC-SA 3.0 CN)國際許可協議進行許可,轉載請註明作者及出處。
本文標題:代碼收藏系列--javascript--日期函數
本文連結:http://www.cnblogs.com/sochishun/p/7150428.html
本文SoChishun (郵箱:14507247#qq.com | 部落格:http://www.cnblogs.com/sochishun/)
發表日期:2017年7月11日

代碼收藏系列--javascript--日期函數

聯繫我們

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