js date扩展方法

/* File Created: 四月 28, 2015 */

//日期加上天数得到新的日期
//dateTemp 需要参加计算的日期,days要添加的天数,返回新的日期,日期格式:YYYY-MM-DD
function getNewDay(dateTemp, days) {
    var dateTemp = dateTemp.split("-");
    var nDate = new Date(dateTemp[1] + ‘-‘ + dateTemp[2] + ‘-‘ + dateTemp[0]); //转换为MM-DD-YYYY格式
    var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);
    var rDate = new Date(millSeconds);
    var year = rDate.getFullYear();
    var month = rDate.getMonth() + 1;
    if (month < 10) month = "0" + month;
    var date = rDate.getDate();
    if (date < 10) date = "0" + date;
    return (year + "-" + month + "-" + date);
}

function getDate(strDate) {
    var date = eval(‘new Date(‘ + strDate.replace(/\d+(?=-[^-]+$)/,
             function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ‘)‘);
    return date;
}
//重写toString方法,将时间转换为Y-m-d H:i:s格式
Date.prototype.toString = function () {
    return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
}
//格式化时间字符串
Date.prototype.toFormatString = function (format) {
    if (format == "") {
        return this.toString();
    }
    var str = ‘‘;
    str = format.replace(/Y|y/, this.getFullYear())
                .replace(/M|m/, this.getMonth() + 1)
                .replace(/D|d/, this.getDate())
                .replace(/H|h/, this.getHours())
                .replace(/I|i/, this.getMinutes())
                .replace(/S|s/, this.getSeconds());
    return str;
}
//在当前时间上添加年数
Date.prototype.addYear = function (years) {
    var cyear = this.getFullYear();
    cyear += years;
    this.setYear(cyear);
    return this;
}
//在当前时间上添加天数
Date.prototype.addDay = function (days) {
    var cd = this.getDate();
    cd += days;
    this.setDate(cd);
    return this;
}
//在当前时间上添加月数
Date.prototype.addMonth = function (months) {
    var cm = this.getMonth();
    cm += months;
    this.setMonth(cm);
    return this;
}
//将php时间格式(Y-m-d H:i:s)转化为js日期对象
function phpDateToJsDate(phpDate) {
    if (phpDate == "") {
        return new Date();
    }
    return new Date(Date.parse(phpDate.replace(/-/g, "/")));
}
时间: 2024-07-29 13:30:30

js date扩展方法的相关文章

JS,JQuery的扩展方法

转 http://blog.csdn.net/tuwen/article/details/11464693 //JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展            var aClass = function(){} //1 定义这个类的静态方法            aClass.sayHello = function(){                alert('say hello');            } //2 定义这个类对象的对象方法   

JQ 扩展方法

//JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展             var aClass = function(){} //1 定义这个类的静态方法             aClass.sayHello = function(){                 alert('say hello');             } //2 定义这个类对象的对象方法             aClass.prototype.protoSayHello = function

jQuery,js如何扩展自定义方法

(jQuery.fn.myMethod=function () { alert('myMethod'); }) (function ($) { $.fn.extend({ myMethod : function () { alert('myMethod'); } }) })(jQuery) js String扩展方法 String.prototype.repalceA=function(){ return this.replace(/a/g,'A') } 'asdasdasd'.repalceA

JS扩展方法

JS扩展方法与C#的扩展方法非常相似,也是可以链式调用的,也是通过对某个类的扩展写法来实现.这个东西非常好用,如果将预先写好的方法放到一个js里面引用的话,那么后面写js将非常有趣. 下面给出一个例子: <head> <title>测试JS扩展方法</title> <script type="text/javascript"> // 合并多个空白为一个空白 String.prototype.ResetBlank = function()

js Date 时间格式化的扩展

js Date 时间格式化的扩展: 1 Date.prototype.format = function (fmt) { 2 var o = { 3 "M+": this.getMonth() + 1, //月 4 "d+": this.getDate(), //日 5 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //时 6 "H+": this.g

JS扩展方法——字符串trim()

转自:http://www.cnblogs.com/kissdodog/p/3386480.html <head> <title>测试JS扩展方法</title> <script type="text/javascript"> // 合并多个空白为一个空白 String.prototype.ResetBlank = function() { //对字符串扩展 var regEx = /\s+/g; return this.replace(

node.js 和 node-webkit C++扩展方法

构建node扩展的方法: 1,安装node.js 版本为0.10.24.msi 2,安装node-gyp, npm install -g node-gyp 3,构建 hello工程 cd 到 node-gyp-hello\src node-gyp configure node-gyp build 构建node-webkit扩展的方法: 1,安装node.js 版本为0.10.24.msi 2,安装nw-gyp, npm install -g  nw-gyp 3,构建 hello工程 cd 到 n

EasyUI扩展方法 + jutil.js

?          EasyUI扩展方法 + jutil.js             博客分类: jQueryEasyUi EasyUI扩展方法: 1.我想指定textarea的行,但editor:{type:'textarea', options: {rows:'4'}}这样写不行.请问大家怎么配置才是指定行的啊? 配置Textarea不可拖动变大变小:   只能自己扩展 textarea 才能实现,配置不用修改,直接扩展就行 Js代码   jQuery.extend(jQuery.fn.

在MVC中My97date应用C#扩展方法

项目中用的my97date,其他字段是用 @Html.EditorFor(model => model.字段)绑定的,但日历控件是用<input />绑定的,但这样写并不是特别好. 为了也能用@HTML绑定属性,用了扩展方法.代码如下: public static class My97DatePicker { private static string defaultFormat = "yyyy-MM-dd"; /// <summary> /// 使用特定