js 日期格式化及日期增减
//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 日期格式化及日期增减的更多相关文章
- C# 日期格式化以及日期常用方法
一.日期格式化 1.ToString() d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中某天的缩写名称,在 AbbreviatedDayNam ...
- Mysql 日期格式化 复杂日期区间查询
前言 最近在做项目涉及到Mysql的复杂日期查询,日期查询其实在数据库中查询其实还是用的挺多的,比如查询开始日期到结束日期的区间信息,查询日期小于有效日期的信息,查询当天的日期,明天的日期,做比较等. ...
- js解析格式化json日期
代码: function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseIn ...
- js返回格式化的日期(年-月-日)
var d = new Date(); var str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate ...
- 161226、js日期格式化
JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...
- JS 日期格式化
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"& ...
- 【JavaScript】 knockout.js 日期格式化借助【momentjs】
源:Knockout.js 日期格式化 源:momentjs
- js日期格式化
<html> <head> <script> function test(){ //Js获取当前日期时间及其它操作 var myDate = new Date(); ...
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
随机推荐
- Linux 远程登录ssh服务器
1.安装ssh服务器 sudo apt-get install openssh-server 2.在另一端输入ssh IP及密码(或ssh 用户名@IP)就可以远程登录到IP所在计算机
- cmake 简学
https://www.cnblogs.com/cv-pr/p/6206921.html
- Angular CLI
简介 ng 官方命令行 Angular CLI 自己的官方文档 https://github.com/angular/angular-cli/wiki/new 常用代码 临时代码 ng new ...
- 动态时间规整DTW(Dynamic Time Warping )
动态时间规整DTW(Dynamic Time Warping ) 原文:https://blog.csdn.net/raym0ndkwan/article/details/45614813 算法笔记- ...
- 3dsmax2017卸载/安装失败/如何彻底卸载清除干净3dsmax2017注册表和文件的方法
3dsmax2017提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2017失败提示3dsmax2017安装未完成,某些产品无法安装,也有时候想重新 ...
- Ubuntu 18.04上搭建FTP服务器
1.准备工作需要安装并运行的Ubuntu Server 18.04系统.当然还需要一个具有sudo权限的账号. 2.安装VSFTPVSFTP程序位于标准存储库中,因此可以使用单个命令删除安装.打开终端 ...
- 基于CommonKADS方法论实现知识库系统
说明:本文是Knowledge-based systems with thecommonKADS method文章的翻译. 一.知识库系统的背景 1. 什么是知识库系统(KBS) 知识库系统是人工智能 ...
- DIV+CSS常见面试题
1.!important拥有最高的优先级,几乎所有浏览器都支持!important,除了IE6(不完全支持) 例1(IE6支持,颜色为#e00): .cssClass{color:#e00!impor ...
- FocusBI: 数据仓库 (原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...
- [H5表单]一些html5表单知识及EventUtil对象完善
紧接着上面的文章,一开始准备一篇文章搞定,后来看到,要总结的东西还不少,干脆,把上面文章拆成两部分吧,这部分主要讲讲表单知识! 表单知识 1.Html5的autofocus属性. 有个这个属性,我们不 ...