/* 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. 基于form表单submit提交不跳转

    方法一:target <html> <body> <form action="" method="post" target=&qu ...

  2. 处理nginx访问日志,筛选时间大于1秒的请求

    #!/usr/bin/env python ''' 处理访问日志,筛选时间大于1秒的请求 ''' with open('test.log','a+',encoding='utf-8') as f_a: ...

  3. Docker使用入门

    docker images 查看本地镜像 docker ps -a  查询容器 docker ps -l  查询最近使用容器 docker rm CONTAINER_ID 删除容器 docker rm ...

  4. Python装饰器探究——装饰器参数

    Table of Contents 1. 探究装饰器参数 1.1. 编写传参的装饰器 1.2. 理解传参的装饰器 1.3. 传参和不传参的兼容 2. 参考资料 探究装饰器参数 编写传参的装饰器 通常我 ...

  5. TouTiao开源项目 分析笔记2

    1.Constant常量定义类 1.1.源代码 public class Constant { public static final String USER_AGENT_MOBILE = " ...

  6. 获取<考试>博文密码!o(*≧▽≦)ツ

    就是CJ高二组通用的密码 如果你想知道,请联系QQ,3057244225,或者直接面对面问博主(...) 是我们的内部材料,原创题目是不能外传的.请谅解. 当然如果是原题的话我们是不会上锁的啦

  7. 让NVelocity做更多的事,VS Extension+NVelocity系列

    我不知道园子里到底有多少人喜欢使用NVelocity这个模板引擎,其实说实话,如果现在让我选,我对Razor的喜好要比NVelocity或者T4等等的模板引擎更多一些,当然了,个人看法而已.只是我在公 ...

  8. Activiti入门 -- 轻松解读数据库

    相关文章: <史上最权威的Activiti框架学习指南> <Activiti入门 --环境搭建和核心API简介> 在Activiti中,相对前身JBPM基础上又额外多了5张,框 ...

  9. 《Cracking the Coding Interview》——第14章:Java——题目1

    2014-04-26 18:20 题目:从继承的角度,把构造函数设成private有什么意义? 解法:就不能继承了.单体模式里也这么干,目的是为了不让使用者自主生成对象,进行限制. 代码: // 14 ...

  10. java日期格式化(util包下转成sql包下)

    package test; import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner; publ ...