123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- (function ( $, w) {
- var d = {
- nowYear: '',
- nowMonth: '',
- nowDay: '',
- nowDayOfWeek: '',
- isNextM: false,
- init: function (type) {
- var now = new Date(),
- getDate = '';
- this.isNextM = false;
- this.nowYear = now.getFullYear();
- this.nowMonth = now.getMonth();
- this.nowDay = now.getDate();
- this.nowDayOfWeek = now.getDay()==0?7:now.getDay();
- switch (type) {
- case 1:
- getDate = this.getWeekStartDate();
- break;
- case 2:
- getDate = this.getWeekEndDate();
- break;
- case 3:
- getDate = this.getMonthStartDate();
- break;
- case 4:
- getDate = this.getMonthEndDate();
- break;
- case 5:
- getDate = this.getDateRange(1);
- break;
- case 6:
- getDate = this.getDateRangeNew();
- break;
- }
- return getDate;
- },
- getWeekStartDate: function () { //获得本周的开始日期
- var weekStartDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek + 1);
- return this.formatDate(weekStartDate);
- },
- getWeekEndDate: function () { //获得本周的结束日期
- var weekEndDate = new Date(this.nowYear, this.nowMonth, this.nowDay + (6 - this.nowDayOfWeek + 1));
- return this.formatDate(weekEndDate);
- },
- getMonthStartDate: function (){//获得本月的开始日期
- var monthStartDate = new Date(this.nowYear, this.nowMonth, 1);
- return this.formatDate(monthStartDate);
- },
- getMonthEndDate: function () {//获得本月的结束日期
- var monthEndDate = new Date(this.nowYear, this.nowMonth, this.getMonthDays(this.nowMonth));
- return this.formatDate(monthEndDate);
- },
- getMonthDays: function (myMonth) { //获得某月的天数
- var monthStartDate = new Date(this.nowYear, myMonth, 1);
- var monthEndDate = new Date(this.nowYear, myMonth + 1, 1);
- var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
- return days;
- },
- getWeeksNum: function (){//获取一个月有几周
- var day=new Date(),
- lastCurDay = new Date(day.getFullYear(), day.getMonth() + 1, 0),//获取当前月最后一天时间
- a = lastCurDay.getFullYear(),
- b = lastCurDay.getMonth(),
- c = lastCurDay.getDate();
- var date = new Date(a, b, c),
- w = date.getDay(),
- d = date.getDate();
- return Math.ceil(
- (d + 6 - w) / 7
- );
- },
- getYear: function () {//获取签约年
- var nd = new Date(),
- year = nd.getFullYear(),
- cd = new Date( year + '', '06', '30', '23', '59', '59');
- if (nd.getTime() <= cd.getTime()) {
- year -= 1;
- }
- return year;
- },
- getDateRange: function (n) {//获取本周的所有日期
- var startDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek),
- arr = [];
- for (var i = 1; i <= 7; i++) {
- startDate.setDate(startDate.getDate() + 1);
- var d = [];
- if (n == 2) {
- d = this.formatDateT( startDate, i);
- }
- if (n == 1) {
- d = this.formatDate( startDate, i);
- }
- arr.push(d);
- }
- return arr;
- },
- getDateRangeNew: function () {//获取本周的所有日期
- var startDate = new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek),
- arr = [];
- for (var i = 1; i <= 7; i++) {
- startDate.setDate(startDate.getDate() + 1);
- var d = [];
- d = this.formatDateNew( startDate, i);
- arr.push(d);
- }
- return arr;
- },
- formatDateNew: function ( data, i) {
- var nd = new Date(),
- myMonth = data.getMonth() + 1,
- myDate = data.getDate();
- if (myMonth < 10) {
- myMonth = '0' + myMonth;
- }
- if (myDate < 10) {
- myDate = '0' + myDate;
- }
-
- return myMonth +"-"+ myDate;
- },
- formatDateT: function ( data, i) {
- var nd = new Date(),
- myMonth = data.getMonth() + 1,
- myDate = data.getDate();
- if (myMonth < 10) {
- myMonth = '0' + myMonth;
- }
- if (i == 1) {
- myMonth += '月'
- } else {
- var m1 = nd.getMonth() + 1,
- m2 = data.getMonth() + 1;
- if (!this.isNextM) {
- if (m1 < m2) {
- this.isNextM = true;
- myMonth += '月'
- } else {
- myMonth = '';
- }
- } else {
- myMonth = '';
- }
- }
- if (myDate < 10) {
- myDate = '0' + myDate;
- }
- return myMonth + myDate;
- },
- formatDate: function (date) {
- var myYear = date.getFullYear(),
- myMonth = date.getMonth() + 1,
- myDate = date.getDate();
- if (myMonth < 10) {
- myMonth = '0' + myMonth;
- }
- if (myDate < 10) {
- myDate = '0' + myDate;
- }
- return myYear + '-' + myMonth + '-' + myDate;
- }
- };
- w.$d = d;
- })( jQuery, window);
|