码迷,mamicode.com
首页 > 其他好文 > 详细

获取系统当前时间并格式化

时间:2017-03-30 19:00:10      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:格式化   logs   javascrip   ring   reg   new   mon   format   string   

createTime--2016年9月19日16:36:30
Author:Marydon

声明:非原创

<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>    

 

获取系统当前时间并格式化

标签:格式化   logs   javascrip   ring   reg   new   mon   format   string   

原文地址:http://www.cnblogs.com/Marydon20170307/p/6647426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!