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, "/")));
}
js date扩展方法的更多相关文章
- js Date 函数方法及日期计算
js Date 函数方法 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份 ...
- js jquery 扩展方法
//扩展Array,增加IsInAyyay函数.函数功能:判断数组是否包含某元素 Array.prototype.IsInAyyay=function(e) { for (var i=0;i<t ...
- js常用扩展方法
在日常的开发过程中,经常会碰到javaScript原生对象方法不够用的情况,所以经常会对javaScript原生方法进行扩展.下面就是在实际工作时,经常使用的一些方法,做一下记录,有需要的可以拿去. ...
- js Date 函数方法 和 移动端数字键盘调用
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- 常用的js对象扩展方法
1. 字符串的replaceAll String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!R ...
- js Date扩展Format()函数
Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', ...
- 为js数组扩展方法
(function(global,undefined){ //javascript冒泡排序,直接添加到基础类型Array的原型上 Function.prototype.method = functio ...
- js Date 函数方法
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- JS,JQuery的扩展方法
转 http://blog.csdn.net/tuwen/article/details/11464693 //JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展 ...
随机推荐
- PLC状态机编程第一篇-状态机介绍
状态机的一般概念 过去我在学习PLC编程时,看的大部分书仅仅停留在软件的操作上,没有真正讲述如何组织程序,这里我们讲一个通用的描述控制算法的方法,就是状态机,和SFC很类似,其实SFC也是源于状态机啦 ...
- python之获取微信好友列表并保存文档中
代码如下 from wxpy import * from pprint import pprint #登录微信 bot = Bot() my_friend = bot.friends() f = op ...
- python学习之map函数和reduce函数的运用
MapReduce:面向大型集群的简化数据处理引文 map()函数 Python中的map()函数接收两个参数,一个是调用函数对象(python中处处皆对象,函数未实例前也可以当对象一样调用),另一个 ...
- L2-032 彩虹瓶 (25 分)
L2-032 彩虹瓶 (25 分) 彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里. 假设彩虹瓶里要按顺序装 N 种颜色的小 ...
- 关于学习less后一些感悟
学习了一天的less发现,自己被自己弄的晕头转向,好在是学明白了! 一.写自己的第一个less css样式编程: 网址:http://www.1024i.com/demo/less/document. ...
- Linux下添加桌面快捷方式
这里用Ubuntu下BurpSuite举例 sudo vim /home/user/Desktop/burpsuite.desktop //burpsuite随意起名,系统会系动创建文件 文件写入 # ...
- X-Frame-Options是什么?
错误: Refused to display 'http://xx.com/spot/kline.do' in a frame because it set 'X-Frame-Options' to ...
- UIView和CALayer是什么关系?
UIView显示在屏幕上归功于CALayer,通过调用drawRect方法来渲染自身的内容,调节CALayer属性可以调整UIView的外观,UIView继承自UIResponder,比起CALaye ...
- 图解-Excel的csv格式特殊字符处理方式尝试笔记(个人拙笔)
Excel格式如下.(截图来自,WPS Office) CSV是一种文本格式的Excel文档格式.不支持Excel的字体特效(比如加粗,颜色)等等的保存. 每一行数据用 "\n" ...
- Python 推导式推导序列
推导式是从一个或多个迭代器快速创建序列的方法.它可以将循环和条件判断结合,从而避免冗长的代码. 一.列表推导式 语法: [表达式 for item in 可迭代对象] [表达式 for item in ...