各种时间加减 收藏起来以备后用

   //名称:日期加法函数
//参数:part(year、month、day、hour、minute、second、millisecond)
//返回:Date对象
Date.prototype.add = function(part, value) {
if (!value || isNaN(value)) value = 0;
switch (part) {
case "year":
this.setFullYear(this.getFullYear() + value);
break;
case "month":
this.setMonth(this.getMonth() + value);
break;
case "day":
this.setDate(this.getDate() + value);
break;
case "hour":
this.setHours(this.getHours() + value);
break;
case "minute":
this.setMinutes(this.getMinutes() + value);
break;
case "second":
this.setSeconds(this.getSeconds() + value);
break;
case "millisecond":
this.setMilliseconds(this.getMilliseconds() + value);
break;
default:
}
return this;
}; Date.prototype.addYears = function(value) {
if (!value || isNaN(value)) value = 0;
this.setFullYear(this.getFullYear() + value);
return this;
}; Date.prototype.addMonths = function(value) {
if (!value || isNaN(value)) value = 0;
this.setMonth(this.getMonth() + value);
return this;
}; Date.prototype.addDays = function(value) {
if (!value || isNaN(value)) value = 0;
this.setDate(this.getDate() + value);
return this;
}; Date.prototype.addHours = function(value) {
if (!value || isNaN(value)) value = 0;
this.setHours(this.getHours() + value);
return this;
}; Date.prototype.addMinutes = function(value) {
if (!value || isNaN(value)) value = 0;
this.setMinutes(this.getMinutes() + value);
return this;
}; Date.prototype.addSeconds = function(value) {
if (!value || isNaN(value)) value = 0;
this.setSeconds(this.getSeconds() + value);
return this;
}; Date.prototype.addMilliseconds = function(value) {
if (!value || isNaN(value)) value = 0;
this.setMilliseconds(this.getMilliseconds() + value);
return this;
}; //名称:日期加法函数
//参数:time(日期字符串,示例:12:00:00)
//返回:Date对象
Date.prototype.addTime = function(time) {
var timeRegex = /^([0-1]?\d|2[0-3])(:[0-5]?\d){1,2}$/g;
if (timeRegex.test(time)) {
var value = Date.parse("1970/1/1 " + time) - Date.parse("1970/1/1");
this.setMilliseconds(this.getMilliseconds() + value);
}
return this;
}; //名称:日期格式化函数
//参数:format(示例:yyyy-MM-dd hh:mm:ss)、zeroize(是否补零)
//返回:日期字符串
Date.prototype.toCustomString = function(format, zeroize) {
if (!zeroize) zeroize = false;
var dy = this.getFullYear();
var dM = this.getMonth() + 1;
var dd = this.getDate();
var dh = this.getHours();
var dm = this.getMinutes();
var ds = this.getSeconds();
var dS = this.getMilliseconds();
var orm = {
"y+": dy.toString(),
"M+": !zeroize ? dM.toString() : dM < 10 ? ‘0‘ + dM : dM.toString(),
"d+": !zeroize ? dd.toString() : dd < 10 ? ‘0‘ + dd : dd.toString(),
"h+": !zeroize ? dh.toString() : dh < 10 ? ‘0‘ + dh : dh.toString(),
"m+": !zeroize ? dm.toString() : dm < 10 ? ‘0‘ + dm : dm.toString(),
"s+": !zeroize ? ds.toString() : ds < 10 ? ‘0‘ + ds : ds.toString(),
"S": dS.toString()
};
for (var i in orm) {
var patt = new RegExp(i);
if (patt.test(format)) {
var item = orm[i];
var ms = format.match(patt);
var result = ms[0];
if (i === "S") {
format = format.replace(result, item);
} else {
format = format.replace(result, item.substr(item.length - result.length));
}
}
}
return format;
};

如果这篇文章对您有帮助,您可以打赏我

技术交流QQ群:15129679

JavaScript之Date日期对象扩展的更多相关文章

  1. JavaScript中Date(日期对象),Math对象--学习笔记

    Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date();  注:初始值为当前时间(当前电脑系统 ...

  2. js动态获取当前系统时间+js字符串转换为date日期对象

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. JS日期对象扩展-日期格式化

    日期对象扩展(日期格式化)yyyy-MM-dd hh:mm:ss.S Date.prototype.format = function(fmt) { var o = { "M+" ...

  4. JS对象 Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒)。 定义一个时间对象 : var Udate=new Date();Date()的首字母须大写

    Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首 ...

  5. JavaScript Date日期对象以及日期格式化方法

    前言 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.util.Date类基础上创建的,为此,Date类型使用自UTC1 ...

  6. JavaScript学习系列博客_30_JavaScript Date 日期对象

    Date - 日期的对象,在JS中通过Date对象来表示一个时间 - 创建一个当前的时间对象 var d = new Date(); - 创建一个指定的时间对象 var d = new Date(&q ...

  7. JS内置对象-String对象、Date日期对象、Array数组对象、Math对象

    一.JavaScript中的所有事物都是对象:字符串.数组.数值.函数... 1.每个对象带有属性和方法 JavaScript允许自定义对象 2.自定义对象 a.定义并创建对象实例 b.使用函数来定义 ...

  8. 第八天:JS内置对象-Date日期对象

    1.Data对象 日期对象用于处理日期和时间 2.获取当日日期    代码如下: <!DOCTYPE html> <html lang="en"><h ...

  9. 初识 Javascript.02 -- Date日期、Math对象、数据类型转换、字符串、布尔Boolean、逻辑运算符、if else 、三元表达式、代码调试方法、

    Date()对象: Date对象用于处理日期和时间. 1.1 Math对象  ◆Math.ceil()   天花板函数    向上取整  只取整数,不足则进1 ◆Math.floor()  地板函数 ...

随机推荐

  1. wpf Assembly.LoadFile dll GetType 反射 抛异常 不具有由 URI 识别的资源。

    public static void LoadViewFromUri(this Window window, string baseUri) { try { var resourceLocater = ...

  2. windows10 更新后要输入2次密码才能进入系统

    解决办法: 设置---账户---登录选项---隐私---更新或重启后,使用我的登录信息自动完成设备设置.(关闭)

  3. Hibernate api 之常见的类(配置类,会话工厂类,会话类)

    1:Configuration :配置管理类对象 1.1:config.configure(): 加载主配置文件的方法(hibernate.cfg.xml) ,默认加载src/hibernate.cf ...

  4. Nginx 响应状态

    ngx.status = ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release)ngx.status = ngx.HTTP_SWITC ...

  5. java集合进行排序的两种方式

    java集合的工具类Collections中提供了两种排序的方法,分别是: Collections.sort(List list) Collections.sort(List list,Compara ...

  6. Codeforces 327E Axis Walking 状压dp

    这题真的有2500分吗... 难以置信... #include<bits/stdc++.h> #define LL long long #define fi first #define s ...

  7. P1141 01迷宫 DFS (用并查集优化)

    题目描述 有一个仅由数字00与11组成的n \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上 ...

  8. C# 创建多级文件夹示例

    string str = ""; ; i < j; i++) { str = str + "\\新建文件夹" ; } System.IO.Director ...

  9. GCC&&GDB在OI中的介绍

    序言 这本来是用Word写的,但是后来我换了系统所以只能用markdown迁移然后写了...... $\qquad$本文主要投食给那些在Windows下活了很久然后考试时发现需要用命令行来操作时困惑万 ...

  10. Codeforces.567E.President and Roads(最短路 Dijkstra)

    题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...