//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. SpringCloud---声明式服务调用---Spring Cloud Feign

    1.概述 1.1 Spring Cloud Ribbon.Spring Cloud Hystrix的使用几乎是同时出现的,Spring Cloud提供了一个更高层次的封装这2个工具类框架:Spring ...

  2. Android Notification 的四种使用方式

    实现通知步骤 一般实现通知需要如下步骤: 1.获取 NotificationManager 实例管理通知: 2.实例 Notification 对象: 3.管理事件 Intent: 4.发送通知. 注 ...

  3. ffmpeg intro - pull and push

    ffmpeg -i rtmp://rtmp.test.com/live/livestream -c:v copy -c:a copy -f flv rtmp://172.31.11.53/myhls/ ...

  4. 对接京东jos遇到的坑 记录一下。方便查询

    坑很多,有一些忘记了.文档乱的很,有问题可以私信我一下我看能不能想起来. 坑一.添加商品接口. {"error_response": {"code":" ...

  5. Oracle Schema

    1.这是Schema的definition: A schema is a collection of database objects (used by a user.) Schema objects ...

  6. 解析ES6 Promise

    ES6 Promise 概念之类的,大概读者都应该有所知道,接下来我们直入终点. 先让我们来看看什么是Promise吧,他是一个object,类,arry,function? 首先,学习它的时候应该讲 ...

  7. CSS生成小三角

    前言:小三角的应用场景:鼠标移动到某个按钮上面,查看信息详情时,信息详情弹出框有时候会需要一个小三角. 代码如下: <div id='triangle'></div> #tri ...

  8. 用泛型T替代object做为万能参数传递

    using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...

  9. jar包读取配置文件

    读取jar包内配置文件: Properties config = new Properties(); InputStream in = this.getClass().getClassLoader() ...

  10. codeforces 675 C ——Money Transfers——————【思维题】

    Money Transfers time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...