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

    1.拼凑

         var myDate = new Date();
                var weekday=new Array(7)
                weekday[0]="星期日"
                weekday[1]="星期一"
                weekday[2]="星期二"
                weekday[3]="星期三"
                weekday[4]="星期四"
                weekday[5]="星期五"
                weekday[6]="星期六"
                var currDate = myDate.getYear()+1900+"年"+myDate.getMonth()+1+"月"+myDate.getDate()+"日 "+weekday[myDate.getDay()];
                document.getElementById("currDate").innerHTML = currDate;

        效果:2016年01月13日 星期三

    2.自定义

        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;
                }
                var currDate = new Date().Format("yyyy-MM-dd hh:mm:ss");
                document.getElementById("currDate").innerHTML = currDate;

        效果:2016-01-13 14:58:51

    currDate

  • 相关阅读:
    硬件的那些事
    seaJS学习资料参考
    nodejs前端自动化构建
    移动端开发的坑【持续更新...】
    【retina】手机上 1PX 边框
    【面试季之三】IE6兼容问题
    【面试季二】前端性能优化
    【面试季一】若干前端面试题
    【面试的坑】行内元素是否可以设置宽高
    Bootstrap和IE何时能相亲相爱啊~
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5127336.html
Copyright © 2011-2022 走看看