zoukankan      html  css  js  c++  java
  • js 时间日期函数小结

    Date.prototype.format = function(format){
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(), //day
    "h+" : this.getHours(), //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3), //quarter
    "S" : this.getMilliseconds() //millisecond
    }

    if(/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
    if(new RegExp("("+ k +")").test(format)) {
    format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    }
    }
    return format;
    }

    //使用方法
    var now = new Date();
    var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
    //使用方法2:
    var testDate = new Date();
    var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
    alert(testStr);
    //示例:
    alert(new Date().Format("yyyy年MM月dd日"));
    alert(new Date().Format("MM/dd/yyyy"));
    alert(new Date().Format("yyyyMMdd"));
    alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));

    js求时间差
    var date1=new Date(); //开始时间
    alert("aa");
    var date2=new Date(); //结束时间
    var date3=date2.getTime()-date1.getTime() //时间差的毫秒数

    //计算出相差天数
    var days=Math.floor(date3/(24*3600*1000))

     

  • 相关阅读:
    OpenJudge 2738 浮点数加法
    OpenJudge 2809 计算2的N次方
    OpenJudge / Poj 1003 Hangover
    OpenJudge 2706 麦森数
    模板:大整数除法
    OpenJudge 2737 大整数除法
    模板:大整数减法
    ES Field Collapsing 字段折叠使用详解
    ES aggregation详解
    一个一站式流式处理云平台解决方案
  • 原文地址:https://www.cnblogs.com/tinya/p/4610805.html
Copyright © 2011-2022 走看看