/* 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, "/")));
}

js date扩展方法的更多相关文章

  1. js Date 函数方法及日期计算

    js Date 函数方法 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份 ...

  2. js jquery 扩展方法

    //扩展Array,增加IsInAyyay函数.函数功能:判断数组是否包含某元素 Array.prototype.IsInAyyay=function(e) { for (var i=0;i<t ...

  3. js常用扩展方法

    在日常的开发过程中,经常会碰到javaScript原生对象方法不够用的情况,所以经常会对javaScript原生方法进行扩展.下面就是在实际工作时,经常使用的一些方法,做一下记录,有需要的可以拿去. ...

  4. js Date 函数方法 和 移动端数字键盘调用

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  5. 常用的js对象扩展方法

    1. 字符串的replaceAll String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!R ...

  6. js Date扩展Format()函数

    Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', ...

  7. 为js数组扩展方法

    (function(global,undefined){ //javascript冒泡排序,直接添加到基础类型Array的原型上 Function.prototype.method = functio ...

  8. js Date 函数方法

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  9. JS,JQuery的扩展方法

    转 http://blog.csdn.net/tuwen/article/details/11464693 //JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展            ...

随机推荐

  1. 简单的for循环实现九九乘法表

    PHP for 循环 语法 for (init counter; test counter; increment counter) { code to be executed; } 参数: init ...

  2. 数据分析处理库Pandas——数值运算

    求和 对每行或每列求和. 均值 对每行或每列求均值. 最大最小值 对每行或每列求最大值或最小值. 中位数 对每行或每列求中位数. 相关系数和协方差 先导入一个DataFram结构的数据,再对这些数据计 ...

  3. Requests库:python实现的简单易用的http库

    1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...

  4. Pandas 文本数据

    Pandas针对字符串配备的一套方法,使其易于对数组的每个元素(字符串)进行操作. 1.通过str访问,且自动排除丢失/ NA值 # 通过str访问,且自动排除丢失/ NA值 s = pd.Serie ...

  5. awk命令例子详解

    awk -F: '{print "Number of dields: "NF}' passwd 字段分隔符设为冒号,所以每条记录的字段数变成7: awk  '{print &quo ...

  6. 笔记-pytho-语法-yield

    笔记-python-语法-yield 1.      yield 1.1.    yield基本使用 def fab(max): n,a,b = 0, 0, 1 while n < max: y ...

  7. 7,vim

    vim与程序员 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体 ...

  8. centos使用--ssh登陆

    1 安装openssh-server yum install openssh-server 2 登录 在配置好ssh后就可以只使用SSH密钥登录而不允许通过密码登录了,方法如下: 修改ssh配置: v ...

  9. 什么情况使用 weak 关键字,相比 assign 有什么不同?

    什么情况使用 weak 关键字? 在 ARC 中,在有可能出现循环引用的时候,往往要通过让其中一端使用 weak 来解决,比如: delegate 代理属性 自身已经对它进行一次强引用,没有必要再强引 ...

  10. 《Cracking the Coding Interview》——第12章:测试——题目3

    2014-04-24 23:28 题目:玩象棋游戏,你要设计一个bool型的方法来检测一个棋子能否移动到指定位置. 解法:不同的棋子有不同的移动规则,那么应该采取棋子基类实现接口,各个棋子子类来实现的 ...