zoukankan      html  css  js  c++  java
  • 2_JavaScript日期格式化

         

    第二章 JavaScript 时间格式化

    2.1 Ticks 转换为常规日期

    2.2 常规日期格式化

    <input type="button" value="时间格式化" onclick="datetimeFormart('/Date(1388505600000)/')" />

    <input type="button" value="时间格式化" onclick="datetimeFormart('/Date(1388505600000)/')" />

    <script type="text/javascript">

     

    Date.prototype.format = function (timeStr) {

    var o = {

    "M+": this.getMonth() + 1,

    "d+": this.getDate(),

    "H+": this.getHours(),

    "m+": this.getMinutes(),

    "s+": this.getSeconds(),

    "f+": this.getMilliseconds()

    }

    if (/(y+)/.test(timeStr))

    timeStr = timeStr.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

     

    for (var k in o)

    if (new RegExp("(" + k + ")").test(timeStr))

    timeStr = timeStr.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

     

    return timeStr;

    }

     

     

     

    function datetimeFormart(str) {

    var obj1 = {};

    obj1 = eval("(" + '{data:"/Date(1388505600000)/"}' + ")");

     

    //alert(obj1.data);

    var reg = /d+/g;

    var numArr = obj1.data.match(reg);

    // alert(numArr.length);

    if (numArr.length > 0) {

    // alert(numArr[0]);

    }

     

    var d = new Date(parseInt(numArr[0]));

    //alert(d);

    //alert(d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes(), d);

     

    alert(d.format("MM"));

    alert(d.format("yyyy-MM-dd HH:mm:ss"));

    }

    </script>

  • 相关阅读:
    UART中RTS、CTS
    Verdi:内存不足
    SV学习之interface
    perl学习之:@_ $_
    perl学习之:package and module
    代码变成可执行程序期间,编译器做了那些事?
    perl学习之:use & require
    perl学习之:use and require
    8位二进制补码表示整数的最小值是什么,最大值是什么
    深入理解计算机系统
  • 原文地址:https://www.cnblogs.com/llchang/p/5256567.html
Copyright © 2011-2022 走看看