UNPKG

mrcio-ui

Version:

193 lines (174 loc) 6.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._Date = undefined; var _react = require("react"); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Date 日期小工具 * @Author gufuyan * @Date 2018-08-30 */ var DateFn = //函数参数默认值 function DateFn() { var _this = this; _classCallCheck(this, DateFn); this.format = function (date, type) { var formatCon = [{ format: "yyyy-MM-dd", resFormat: function resFormat(year, month, day) { return year + "-" + month + "-" + day; } }, { format: "yyyy/MM/dd", resFormat: function resFormat(year, month, day) { return year + "/" + month + "/" + day; } }, { format: "yyyy.MM.dd", resFormat: function resFormat(year, month, day) { return year + "." + month + "." + day; } }, { format: "yyyy-MM-dd HH:mm:ss", resFormat: function resFormat(year, month, day, hour, minute, second) { return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; } }]; var _type = type || "yyyy-MM-dd"; var _date = date instanceof Date ? date : new Date(); var year = _date.getFullYear(), month = _this.formatTen(_date.getMonth() + 1), day = _this.formatTen(_date.getDate()), hour = _this.formatTen(_date.getHours()), minute = _this.formatTen(_date.getMinutes()), second = _this.formatTen(_date.getSeconds()); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = formatCon[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var o = _step.value; if (_type == o.format) { return o.resFormat(year, month, day, hour, minute, second); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } }; this.getWeek = function (date) { var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "cn"; var _date = date instanceof Date ? date : new Date(); var day = _date.getDay(); //0,1,2,3,4,5,6 // type cn:"中国标准日期 周一到周日" en:"美国标准日期 周日到周六" day = type == "cn" ? day - 1 : type == "en" ? day : day; var dayTime = _date.getTime(); var weekList = []; for (var i = 0; i <= 6; i++) { var d = new Date(dayTime - (day - i) * 86400000); weekList.push({ date: _this.format(d, "yyyy-MM-dd"), week: _this.dayCon["_" + d.getDay()] }); } return weekList; }; this.dayCon = { _0: "星期日", _1: "星期一", _2: "星期二", _3: "星期三", _4: "星期四", _5: "星期五", _6: "星期六" }; this.formatTen = function (num) { return parseInt(num) > 9 ? num + "" : "0" + parseInt(num); }; // 能被4整除,但不能被100整除的年份 和 能被400整除的年份 是 闰年 ;反之 平年 //平年2月28天,闰年2月29天 this.yearType = function (year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? "闰年" : "平年"; }; //获取某月份最后一天 方法一 month 1~12 this.monthMaxDay = function (year, month) { return new Date(year, month, 0).getDate(); }; //获取某月份最后一天 方法二 month 1~12 this.getMonthMaxDay = function (year, month) { var isLeap = function isLeap(year) { return year % 100 == 0 ? year % 400 == 0 ? 1 : 0 : year % 4 == 0 ? 1 : 0; }, //平年 瑞年 monthDays = [31, 28 + isLeap(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; return monthDays[month - 1]; }; }; var _Date = new DateFn(); exports._Date = _Date; //原生封装 // const _Date = function () { // //获取星期 ,getDay()的值有: 1一,2二,3三,4四,5五,6六,0日 // this.dayCon = { // _0: "星期日", // _1: "星期一", // _2: "星期二", // _3: "星期三", // _4: "星期四", // _5: "星期五", // _6: "星期六", // }; // this.formatTen = (num) =>num > 9 ? (num + "") : ("0" + num); // }; // // _Date.prototype.format = (date, type) => { // let __Date = new _Date(); // let formatCon = [ // {format: "yyyy-MM-dd", resFormat: (year, month, day) => `${year}-${month}-${day}`}, // {format: "yyyy/MM/dd", resFormat: (year, month, day) => `${year}/${month}/${day}`}, // {format: "yyyy.MM.dd", resFormat: (year, month, day) => `${year}.${month}.${day}`}, // { // format: "yyyy-MM-dd HH:mm:ss", // resFormat: (year, month, day, hour, minute, second) =>`${year}-${month}-${day} ${hour}:${minute}:${second}` // } // ]; // let _type = type || "yyyy-MM-dd"; // let _date = date instanceof Date ? date : new Date(); // let year = _date.getFullYear(), // month = __Date.formatTen(_date.getMonth() + 1), // day = __Date.formatTen(_date.getDate()), // hour = __Date.formatTen(_date.getHours()), // minute = __Date.formatTen(_date.getMinutes()), // second = __Date.formatTen(_date.getSeconds()); // for (let o of formatCon) { // if (_type == o.format) { // return o.resFormat(year, month, day, hour, minute, second) // } // } // }; // // _Date.prototype.getWeek = (date, type) => { // let __Date = new _Date(); // let _date = date instanceof Date ? date : new Date(); // let day = _date.getDay();//1 2 // let dayTime = _date.getTime(); // let weekList = []; // for (let i = 0; i <= 6; i++) { // let d = new Date(dayTime - (day - i ) * 86400000); // weekList.push({ // date: __Date.format(d, "yyyy-MM-dd"), // week: __Date.dayCon[`_${d.getDay()}`] // }) // } // return weekList // }; // // let _Date = new _Date(); //# sourceMappingURL=date.js.map