javascript时间函数

//js格式化时间
//"yyyy-MM-dd hh:mm:ss"
Date.prototype.format = function (format) {
    var date = {
        "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+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + ‘‘).substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                            ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}
//时间加天数
Date.prototype.addDays = function (day, format) {
    this.setDate(this.getDate() + day);
    return this.format(format);
}
//时间加周数
Date.prototype.addWeeks = function (w) {
    this.addDays(w * 7);
};
//时间加月数
Date.prototype.addMonths = function (m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);

    if (this.getDate() < d)
        this.setDate(0);
};
//时间加年数
Date.prototype.addYears = function (y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);

    if (m < this.getMonth()) {
        this.setDate(0);
    }
};
//根据日期,起得第几周
Date.prototype.getWeek = function () {
    return new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[this.getDay()];
}
//获取参数,如:字符串(‘?a=hello&b=world‘),window.location.search.parameter(‘a‘);
String.prototype.parameter = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = this.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
时间: 2024-07-30 07:31:24

javascript时间函数的相关文章

javascript 时间操作

javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=new Date(milliseconds) //距离起始时间1970年1月1日的毫秒数 3.dateObj=new Date(datestring) //字符串代表的日期与时间.此字符串可以使用Date.parse()转换,比如"Jannuary 1, 1998 20:13:15" 4.d

Javascript 时间计算函数

Javascript 时间计算函数 <span style="font-size:18px;"> <span style="font-size:18px;">function addZore(x) {if (x < 10) {x = "0" + x;};return x;}//2002-12-18格式 function getCurentDate() { var date = new Date(); var mont

JavaScript的屏幕输出及时间函数

Js屏幕输出 重定向页面输出: 更改输出流,如果在加载时使用则会正常排序,如果在HTML加载完成后使用,如点击事件,则会全部覆盖原HTML的内容. 1 <script> 2 //参数为输出内容 3 document.write("dongxiaodong<br/>东小东"); 4 </script> 弹框网页: 可以实现打开一个类似于提示栏窗口网页. window.open("https://www.cnblogs.com/dongxiao

[javascript]获取系统时间函数

var oDate=new Date(); //初始化系统时间函数 alert(oDate.getHours()); //获取时 alert(oDate.getMinutes()); //获取分 alert(oDate.getSeconds()); //获取秒

JavaScript时间日期函数

//随机数生成器Math.random() 装换为整数parseInt() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b.getMonth()+1; //获取月份b.getDate() //获取天b.getHours() //获取小时b.getMinutes() //获取分钟b.getSeconds() //获取秒数b.getDay() //获取星期几b.getMill

JavaScript Function 函数深入总结

整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下文,私有变量等知识点的深入理解. 函数中的return return 语句可以不带有任何返回值,在这种情况下( return; 或函数中不含 return 语句时),函数在停止执行后将返回 undefiend 值.这种用法一般在需要提前停止函数执行而又不需要返回值的情况下. return false

JavaScript 常用函数总结

javascript函数:  ·常规函数  ·数组函数  ·日期函数  ·数学函数  ·字符串函数 .cookie函数 1.常规函数 javascript常规函数包括以下9个函数:  (1)alert函数:显示一个警告对话框,包括一个OK按钮.  (2)confirm函数:显示一个确认对话框,包括OK.Cancel按钮.  (3)escape函数:将字符转换成Unicode码.  (4)eval函数:计算表达式的结果.  (5)isNaN函数:测试是(true)否(false)不是一个数字. 

javascript常用函数整理

javascript函数: 1.禁止鼠标右键菜单代码块 function stop() {     return false; } document.oncontextmenu = stop; //禁用鼠标右键 document.ondragstart = stop; //禁止拖动 document.onselectstart = stop; //禁止选择 2.判断打开的页面是否存在父页面 if (window.top == window.self && window.parent == 

js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScript使用push方法添加一个元素到数组末 JavaScript数组函数unshift、shift、pop、push使用

push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( item1 [,items... ] )参数 参数 描述item1 任意类型添加到当前数组末尾处的元素.items 可选参数/任意类型要添加到当前数组末尾处的其他项,可以有多个.注意:如果添加的元素类型为数组类型(Array),仍然会被当作一个元素看待,只是这个元素是数组类型而已.如果要合并两个数组,请使