用于JS日期格式化,以及简单运算的Date包装工具类
1. [文件] yDate.js
/**
* | yDate.js | Copyright (c) 2013 yao.yl | email: redrainyi@126.com | Date: 2012-09-03 |
*/
(function(global) {
var objectPrototypeToString = Object.prototype.toString;
var isDate = function(value) {
return objectPrototypeToString.call(value) === '[object Date]';
};
var cloneDate = function(pDate, process) {
var vDate = new Date(pDate.getTime());
var year = vDate.getFullYear(), //
month = vDate.getMonth(), //
date = vDate.getDate(), //
hours = vDate.getHours(), //
minutes = vDate.getMinutes(), //
seconds = vDate.getSeconds();//
(!!process) && process(vDate, year, month, date, hours, minutes, seconds);
return vDate;
};
var parseDate = function(dateString, pattern) {
try {
var matchs1 = (pattern || (dateString.length === 10 ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss')).match(/([yMdHsm])(\1*)/g);
var matchs2 = dateString.match(/(\d)+/g);
if (matchs1.length === matchs2.length) {
var $d = new Date(1970, 0, 1);
for (var i = 0; i < matchs1.length; i++) {
var $i = parseInt(matchs2[i], 10);
switch (matchs1[i].charAt(0) || '') {
case 'y' :
$d.setFullYear($i);
break;
case 'M' :
$d.setMonth($i - 1);
break;
case 'd' :
$d.setDate($i);
break;
case 'H' :
$d.setHours($i);
break;
case 'm' :
$d.setMinutes($i);
break;
case 's' :
$d.setSeconds($i);
break;
default :
//
}
}
return $d;
}
} catch (err) {
alert(err)
}
return null;
};
var formatDate = (function() {
var SIGN_RG = /([yMdHsm])(\1*)/g;
function padding(s, len) {
var len = len - (s + "").length;
for (var i = 0; i < len; i++) {
s = "0" + s;
}
return s;
}
return function(value, pattern) {
if (!isDate(value)) {
return '';
}
try {
pattern = pattern || 'yyyy-MM-dd HH:mm:ss';
return pattern.replace(SIGN_RG, function($0) {
switch ($0.charAt(0)) {
case 'y' :
return padding(value.getFullYear(), $0.length);
case 'M' :
return padding(value.getMonth() + 1, $0.length);
case 'd' :
return padding(value.getDate(), $0.length);
case 'w' :
return value.getDay() + 1;
case 'H' :
return padding(value.getHours(), $0.length);
case 'm' :
return padding(value.getMinutes(), $0.length);
case 's' :
return padding(value.getSeconds(), $0.length);
case 'q' :
return Math.floor((this.getMonth() + 3) / 3);
default :
return '';
}
});
} catch (err) {
return '';
}
};
})();
var getActualMaximum = function(date) {
var vDate = new Date(date.getTime());
vDate.setMonth(vDate.getMonth() + 1);
vDate.setDate(0);
return vDate.getDate();
}
var YDate = function() {
var p0 = arguments[0];
var p1 = arguments[1];
if (typeof p0 === 'number' && isFinite(value)) {
this.vDate = new Date(p0);//millis
} else if (isDate(p0)) {
this.vDate = new Date(p0.getTime());
} else if (typeof p0 === 'string') {
if (typeof p1 === 'string' || typeof p1 === 'undefined') {
this.vDate = parseDate(p0, p1);
}
} else if (arguments.length == 0) {
this.vDate = new Date();
} else {
throw 'YDate Constructor Error!';
}
this.$year = this.vDate.getFullYear();
this.$month = this.vDate.getMonth();
this.$date = this.vDate.getDate();
this.$hours = this.vDate.getHours();
this.$minutes = this.vDate.getMinutes();
this.$seconds = this.vDate.getSeconds();
this.$day = this.vDate.getDay();
};
YDate.prototype = {
plusYear : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setFullYear(year + value);
}));
},
plusMonth : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setMonth(month + value);
}));
},
plusDate : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setDate(date + value);
}));
},
plusHours : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setHours(hours + value);
}));
},
plusMinutes : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setMinutes(minutes + value);
}));
},
plusSeconds : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setSeconds(seconds + value);
}));
},
minusYear : function(value) {
return this.plusYears(-value);
},
minusMonth : function(value) {
return this.plusMonths(-value);
},
minusDate : function(value) {
return this.plusDate(-value);
},
minusHours : function(value) {
return this.plusHours(-value);
},
minusMinutes : function(value) {
return this.plusMinutes(-value);
},
minusSeconds : function(value) {
return this.plusSeconds(-value);
},
setYear : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setFullYear(value);
}));
},
setMonth : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setMonth(value);http://www.huiyi8.com/clxgt/
}));窗帘效果图
},
setDate : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setDate(value);
}));
},
setHours : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setHours(value);
}));
},
setMinutes : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setMinutes(value);
}));
},
setSeconds : function(value) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
vDate.setSeconds(value);
}));
},
getYear : function() {
return vDate.getFullYear();
},
getMonth : function() {
return vDate.getMonth();
},
getDate : function() {
return vDate.getDate();
},
getHours : function() {
return vDate.getHours();
},
getMinutes : function() {
return vDate.getMinutes();
},
getSeconds : function() {
return vDate.getSeconds();
},
getDayOfWeek : function() {
return vDate.getDay();
},
toDate : function() {
return cloneDate(this.vDate);
},
calculate : function(expression) {
},
clone : function() {
return new YDate(cloneDate(this.vDate));
},
getBegin : function(field) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
switch (field) {
case 'yyyy' ://year
vDate.setMonth(0);
vDate.setDate(1);
vDate.setHours(0);
vDate.setMinutes(0);
vDate.setSeconds(0);
break;
case 'MM' ://month
vDate.setDate(1);
vDate.setHours(0);
vDate.setMinutes(0);
vDate.setSeconds(0);
case 'dd' ://date
vDate.setHours(0);
vDate.setMinutes(0);
vDate.setSeconds(0);
break;
default :
//Ignore
}
}));
},
getEnd : function(field) {
return new YDate(cloneDate(this.vDate, function(vDate, year, month, date, hours, minutes, seconds) {
switch (field) {
case 'yyyy' ://year
vDate.setMonth(11);
vDate.setDate(31);
vDate.setHours(23);
vDate.setMinutes(59);
vDate.setSeconds(59);
break;
case 'MM' ://month
vDate.setDate(getActualMaximum(vDate));
vDate.setHours(23);
vDate.setMinutes(59);
vDate.setSeconds(59);
case 'dd' ://date
vDate.setHours(23);
vDate.setMinutes(59);
vDate.setSeconds(59);
break;
default :
//Ignore
}
}));
},
toString : function(pattern) {
return formatDate(this.vDate, pattern);
}
};
global.YDate = YDate;
})(window);
2. [代码]使用介绍
<!Doctype html>
<html>
<head>
<title>yDate.test</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<script type="text/javascript" src="yDate.js">
</script>
</head>
<body>
<script type="text/javascript">
//创建一个YDate日期对象
var date1 = new YDate('2013-01-01 11:50:20');
alert(date1.toString());
//获得JS Date对象
alert(date1.toDate());
var date2 = new YDate('2012-02-11');
alert(date2.toString());
//format日期对象
alert(date2.toString('yyyy年MM月dd日'));
//获得本月最后时刻的日期
var date3 = date2.getEnd('MM');//yyyy MM dd
alert(date3.toString());
//获得本年最初时刻的日期
var date4 = date2.getBegin('yyyy');
alert(date4.toString());
</script>
</body>
</html>
用于JS日期格式化,以及简单运算的Date包装工具类的更多相关文章
- 161226、js日期格式化
JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...
- js日期格式化 扩展Date()
javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...
- JS 日期格式化
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"& ...
- 【JavaScript】 knockout.js 日期格式化借助【momentjs】
源:Knockout.js 日期格式化 源:momentjs
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
- JS日期格式化(网上转载)
JS日期格式化(网上转载) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <ht ...
- js 日期格式化函数(可自定义)
js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- javascript常用开发笔记:一个简单强大的js日期格式化方法
前言:一直没找到好用的javascript日期格式化工具,自己写了一个,方便以后复用 1.主要功能 (1)支持任意顺序的日期格式排列:yyyy=年,MM=月,dd=日,HH=时,mm=分,ss=秒,s ...
随机推荐
- poj 3083 dfs,bfs
传送门 Children of the Candy Corn Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Tomcat 7 的domain域名配置,Tomcat 修改JSESSIONID
https://blog.csdn.net/catoop/article/details/64581325
- ubuntu下U盘变为只读
原文地址:http://www.cnblogs.com/coding-way/p/4243331.html 首先执行命令: tail -f /var/log/syslog 然后插入有问题的U盘,tai ...
- Spark学习(一) Spark初识
一.官网介绍 1.什么是Spark 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于A ...
- 使用Myeclipse + SVN + TaoCode 免费实现项目版本控制的详细教程
通过Myeclipse + SVN插件 + TaoCOde可以省去代码仓库的租建:同时还可以很好的满足小团队之间敏捷开发的需求.接下来详细介绍整个搭建流程. 首先,介绍所用到的工具: 1,Myecli ...
- 【内核研究】处理者_Handler
虽然MessageQueue提供了直接读/写的函数接口.但对于程序猿来说,一般不直接读/写消息队列.之前了解到,在Looper.loop()函数中.当取出消息后,会回调msg.target对象的han ...
- BUPT复试专题—字符串处理(2016)
题目描述 有以下三种操作. (1)COPY l r(0<=l<=r<n),n代表s串的长度.这个表示将s串从l到r的序列复制到剪贴板t里面,覆盖t字符串. 例如s为abcde ...
- Coursera 机器学习Course Wiki Lecture Notes
https://share.coursera.org/wiki/index.php/ML:Main 包含了每周的Lecture Notes,以便复习回顾的时候使用.
- 静态NAT、动态NAT、PAT(端口多路复用)的配置
静态NAT.动态NAT.PAT(端口多路复用)的配置 NAT的实现方式有三种,即静态转换Static Nat.动态转换Dynamic Nat 和 端口多路复用OverLoad. 静态转换 ( ...
- [Testing] Config jest to test Javascript Application -- Part 1
Transpile Modules with Babel in Jest Tests Jest automatically loads and applies our babel configurat ...