用于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 ...
随机推荐
- gdb 远程调试android进程
原文:http://blog.csdn.net/xinfuqizao/article/details/7955346?utm_source=tuicool 什么是gdb 它是gnu组织开发的一个强大的 ...
- 发布npm包
来源:https://segmentfault.com/a/1190000010398983
- 枚举型变量 ErrorStatus HSEStartUpStatus及使用
ErrorStatus和C语言中的int .char一样,后面定义的HSEStartUpStatus是这个变量.举例,你的ErrorStatus 代表bool类型的0或者1. typedef enum ...
- Codeforces 837 E Vasya's Function
Discription Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = ...
- 阿里云***(java应用)
阿里云***(FQ)实战 前言 因为公司涉及国外业务,依赖于google的map服务,生产环境我们使用的是亚马逊服务器,所以访问google地图没问题,但是国内的开发.测试环境,使用的是阿里云,想 ...
- NIO与传统IO的区别(形象比喻)[转]
传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...
- 微软自带的异步Ajax请求
一.使用步骤 二.示例代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- Win10 - 默认图片查看器恢复
1. 新建一个文本文件“1.txt” 2. 在“1.txt”中添加如下代码,并保存: Windows Registry Editor Version 5.00 ; Change Extension's ...
- yum gd linux
32位操作系统安装命令:[root@localhost ~]# yum install php-gd*64位操作系统安装命令:[root@localhost ~]# yum install php-g ...
- 关于linter
各类代码都有规则格式检查工具,称之为linter 比如:csslint/jslint/eslint/pylint sumlime提供了一个linter的框架SublimeLinter,在里面可以使用各 ...