//Demo:new Date().format("yyyy-MM-dd hh:mm:ss.SSS")
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
return format;
} Date.prototype.addYears = function (num) {
num = parseInt(num); var sYear = this.getFullYear();
var sMonth = this.getMonth();
var sDay = this.getDate(); var eYear = sYear + num;
var eMonth = sMonth;
var eDay = sDay; var eDate = new Date(eYear, eMonth, eDay); while (eDate.getMonth() != eMonth) {
eDay--;
eDate = new Date(eYear, eMonth, eDay);
} var times = eDate.getTime() - new Date(sYear, sMonth, sDay).getTime();
return new Date(new Date().getTime() + times);
}
//Demo:new Date().addMonths(-1)
Date.prototype.addMonths = function (num) {
num = parseInt(num); var sYear = this.getFullYear();
var sMonth = this.getMonth() + 1;
var sDay = this.getDate(); var eYear = sYear;
var eMonth = sMonth + num;
var eDay = sDay;
while (eMonth > 12) {
eYear++;
eMonth -= 12;
}
while (eMonth <= 0) {
eYear--;
eMonth += 12;
} var eDate = new Date(eYear, eMonth - 1, eDay); while (eDate.getMonth() != eMonth - 1) {
eDay--;
eDate = new Date(eYear, eMonth - 1, eDay);
} var times = eDate.getTime() - new Date(sYear, sMonth - 1, sDay).getTime();
return new Date(new Date().getTime() + times);
} Date.prototype.addDates = function (num) {
return this.addHours(num * 24);
}
Date.prototype.addHours = function (num) {
return this.addMinutes(num * 60);
}
Date.prototype.addMinutes = function (num) {
return this.addSeconds(num * 60);
} Date.prototype.addSeconds = function (num) {
num = parseInt(num);
return new Date(new Date().getTime() + num * 1000);
}

js 日期格式化及日期增减的更多相关文章

  1. C# 日期格式化以及日期常用方法

    一.日期格式化 1.ToString() d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 AbbreviatedDayNam ...

  2. Mysql 日期格式化 复杂日期区间查询

    前言 最近在做项目涉及到Mysql的复杂日期查询,日期查询其实在数据库中查询其实还是用的挺多的,比如查询开始日期到结束日期的区间信息,查询日期小于有效日期的信息,查询当天的日期,明天的日期,做比较等. ...

  3. js解析格式化json日期

    代码: function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式    try {        var date = new Date(parseIn ...

  4. js返回格式化的日期(年-月-日)

    var d = new Date(); var str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate ...

  5. 161226、js日期格式化

    JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...

  6. JS 日期格式化

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"& ...

  7. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

  8. js日期格式化

    <html> <head> <script> function test(){ //Js获取当前日期时间及其它操作 var myDate = new Date(); ...

  9. JS获取当前日期时间及JS日期格式化

    Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...

随机推荐

  1. Maven 编译报错

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-t ...

  2. C语言中结构体定义

    struct test { int a; }; /* 定义一个结构体,名字是test,这样就可以使用struct test 来定义变量.比如 struct test a; */ typedef str ...

  3. nginx, flask, wsgi

    原来自己还没搞懂这些. 首先post一个观点: nginx应该是没解析任何东西,就判断是不是http请求,然后转发?或者判断是不是tcp请求,然后转发. 所以给了python后台就可以用wsgi解包. ...

  4. light table 添加行号 更新

    在上一个笔记修改完字体后.再添加上行号

  5. java多线程开发之CyclicBarrier,CountDownLatch

    最近研究了一个别人的源码,其中用到多个线程并行操作一个文件,并且在所有线程全部结束后才进行主线程后面的处理. 其用到java.util.concurrent.CyclicBarrier 这个类. Cy ...

  6. WPF实现无刷新动态切换多语言(国际化)

    1. 在WPF中国际化使用的是 .xaml文件的格式 如图:Resource Dictionary (WPF) 2. 创建默认的语言文件和其他语言文件 这里以英语为默认语言,新建一个 Resource ...

  7. Python基础(3) - 数据类型:2字符串类型

    Python字符串的表示有三种方法: 1.单引号(') >>>a = 'I love python. ' 2.双引号(") >>>a = " I ...

  8. replaceAll的一个bug

    String replaceAll(regex, replacement)函数 , 由于第一个参数支持正则表达式,replacement中出现“$”,会按照$1$2的分组模式进行匹配,当编译器发现“$ ...

  9. ReSharper+Devexpress 删除光标之后的换行

    echo. >"$(ProjectDir)\Properties\licenses.licx" 官方链接

  10. SSIS教程:创建简单的ETL包 -- 2. 添加循环(Adding Looping)

    在第 1 课:创建项目和基本包中,创建了从单个平面文件源中提取数据的包,然后使用查找转换功能对数据进行了转换,最后将数据加载到AdventureWorksDW2012 示例数据库的 FactCurre ...