123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- Date.prototype.format = function(formatStr) {
- var str = formatStr;
- var Week = ['日', '一', '二', '三', '四', '五', '六'];
- str = str.replace(/yyyy|YYYY/, this.getFullYear());
- str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
- str = str.replace(/MM/, this.getMonth() > 8 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
- str = str.replace(/M/g, this.getMonth() + 1);
- str = str.replace(/w|W/g, Week[this.getDay()]);
- str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
- str = str.replace(/d|D/g, this.getDate());
- str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
- str = str.replace(/h|H/g, this.getHours());
- str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
- str = str.replace(/m/g, this.getMinutes());
- str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
- str = str.replace(/s|S/g, this.getSeconds());
- return str;
- }
- function isMobilePhone(n) {
- var reg = /^1[34578]\d{9}$/
- return reg.test(n)
- }
- function isTelPhone(n) {
- var reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/
- return reg.test(n)
- }
- function getDateFromNow(days) {
- if(!days) {
- return new Date().format("yyyy-MM-dd")
- }
- var now = new Date().getTime()
- var diff = 24 * 3600 * 1000 * days
- return new Date(now + diff).format("yyyy-MM-dd")
- }
- function GetRequest() {
- var url = location.search; //获取url中"?"符后的字串
- var theRequest = new Object();
- if(url.indexOf("?") != -1) {
- var str = url.substr(1);
- strs = str.split("&");
- for(var i = 0; i < strs.length; i++) {
- theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
- }
- }
- return theRequest;
- }
- //判断输入字符串是否为空或全是空格
- function isNull(str) {
- if(str == null) return true;
- if(str == "") return true;
- var regu = "^[ ]+$";
- var re = new RegExp(regu);
- var bb = re.test(str);
- return re.test(str);
- }
- /**
- * 获取本周、本季度、本月、上月的开端日期、停止日期
- */
- var now = new Date(); //当前日期
- var nowDayOfWeek = now.getDay(); //今天本周的第几天
- var nowDay = now.getDate(); //当前日
- var nowMonth = now.getMonth(); //当前月
- var nowYear = now.getYear(); //当前年
- nowYear += (nowYear < 2000) ? 1900 : 0; //
- var lastMonthDate = new Date(); //上月日期
- lastMonthDate.setDate(1);
- lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
- var lastYear = lastMonthDate.getYear();
- var lastMonth = lastMonthDate.getMonth();
- //格局化日期:yyyy-MM-dd
- function formatDate(date) {
- var myyear = date.getFullYear();
- var mymonth = date.getMonth() + 1;
- var myweekday = date.getDate();
- if(mymonth < 10) {
- mymonth = "0" + mymonth;
- }
- if(myweekday < 10) {
- myweekday = "0" + myweekday;
- }
- return(myyear + "-" + mymonth + "-" + myweekday);
- }
- //获得某月的天数
- function getMonthDays(myMonth) {
- var monthStartDate = new Date(nowYear, myMonth, 1);
- var monthEndDate = new Date(nowYear, myMonth + 1, 1);
- var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
- return days;
- }
- //获得本季度的开端月份
- function getQuarterStartMonth() {
- var quarterStartMonth = 0;
- if(nowMonth < 3) {
- quarterStartMonth = 0;
- }
- if(2 < nowMonth && nowMonth < 6) {
- quarterStartMonth = 3;
- }
- if(5 < nowMonth && nowMonth < 9) {
- quarterStartMonth = 6;
- }
- if(nowMonth > 8) {
- quarterStartMonth = 9;
- }
- return quarterStartMonth;
- }
- //获得本周的开端日期
- function getWeekStartDate() {
- var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
- return formatDate(weekStartDate);
- }
- //获得本周的停止日期
- function getWeekEndDate() {
- var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
- return formatDate(weekEndDate);
- }
- //获得本月的开端日期
- function getMonthStartDate() {
- var monthStartDate = new Date(nowYear, nowMonth, 1);
- return formatDate(monthStartDate);
- }
- //获得本月的停止日期
- function getMonthEndDate() {
- var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
- return formatDate(monthEndDate);
- }
- //获得上月开端时候
- function getLastMonthStartDate() {
- var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
- return formatDate(lastMonthStartDate);
- }
- //获得上月停止时候
- function getLastMonthEndDate() {
- var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
- return formatDate(lastMonthEndDate);
- }
- //获得本季度的开端日期
- function getQuarterStartDate() {
- var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
- return formatDate(quarterStartDate);
- }
- //或的本季度的停止日期
- function getQuarterEndDate() {
- var quarterEndMonth = getQuarterStartMonth() + 2;
- var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
- return formatDate(quarterStartDate);
- }
- //获得本年的开始日期
- function getYearStartDate() {
- //获得当前年份4位年
- var currentYear=new Date().getFullYear();
- //本年第一天
- var currentYearFirstDate=new Date(currentYear,0,1);
- return formatDate(currentYearFirstDate);
- }
- //获得本年的结束日期
- function getYearEndDate() {
- //获得当前年份4位年
- var currentYear=new Date().getFullYear();
- //本年最后
- var currentYearLastDate=new Date(currentYear,11,31);
- return formatDate(currentYearLastDate);
- }
|