123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- export default class DateFormat{
- constructor(ms, formatStr){
- this.setTime(ms, formatStr);
- }
- set year(y){ if(this.y !== y){ this.__date.setFullYear(y); this.setTime(this.__date); } }
- get year(){ return this.y; }
- set month(y){ if(this.m !== y){ this.__date.setMonth(y-1); this.setTime(this.__date); } }
- get month(){ return this.m; }
- set day(y){ if(this.d !== y){ this.__date.setDate(y); this.setTime(this.__date); } }
- get day(){ return this.d; }
- set hour(y){ if(this.h !== y){ this.__date.setHours(y); this.setTime(this.__date); } }
- get hour(){ return this.h; }
- set minute(y){ if(this.f !== y){ this.__date.setMinutes(y); this.setTime(this.__date); } }
- get minute(){ return this.f; }
- set second(y){ if(this.s !== y){ this.__date.setSeconds(y); this.setTime(this.__date); } }
- get second(){ return this.s; }
- set msecond(y){ if(this.ms !== y){ this.__date.setMilliseconds(y); this.setTime(this.__date); } }
- get msecond(){ return this.ms; }
- get week(){ return this.w; }
- setTime(ms, formatStr){
- if(formatStr && typeof ms === 'string'){
- let result;
- if(this.__analysis){
- result = this.__analysis(ms, formatStr);
- }
- if(!result){
- console.log('匹配时间失败,默认设置为当前时间');
- ms = '';
- }else{
- ms = result;
- }
- }
- let date;
- if(ms instanceof Date){
- date = ms;
- }else if(typeof ms === 'number'){
- date = new Date(ms);
- }else if( parseInt(ms) > 0 ){
- date = new Date(parseInt(ms));
- }else{
- if(typeof ms === 'object' && ms.y && ms.m && ms.d){
- date = new Date(ms.y, ms.m-1, ms.d, ms.h || 0, ms.f || 0, ms.s || 0, ms.ms || 0);
- }else{
- date = new Date();
- }
- }
- this.y = date.getFullYear(),
- this.m = date.getMonth() + 1,
- this.d = date.getDate(),
- this.h = date.getHours(),
- this.f = date.getMinutes(),
- this.s = date.getSeconds(),
- this.ms = date.getMilliseconds(),
- this.w = date.getDay();
- if(this.w === 0){
- this.w = 7;
- }
- this.timestamp = date.getTime();
- this.__date = date;
-
- return this;
- }
- toString(formatStr){
- if(formatStr){
- if(formatStr === 'time'){
-
- return this.fillChar(this.h, 2) + ':' + this.fillChar(this.f, 2) + ':' + this.fillChar(this.s, 2);
- }else if(formatStr === 'date'){
-
- return this.y + '-' + this.m + '-' + this.d;
- }else{
- return this.__replaceTime(formatStr);
- }
-
- }else{
-
- return this.y + '-' + this.m + '-' + this.d + ' ' + this.fillChar(this.h, 2) + ':' + this.fillChar(this.f, 2) + ':' + this.fillChar(this.s, 2);
- }
- }
- valueOf(){
- return this.toString();
- }
-
- __replaceTime(format){
- var str =
- 'y{4}|Y{4}|y{2}|Y{2}|mm|MM|0m|dd|DD|0d|hh|HH|0h|ff|FF|0F|0f|ss|SS|0S|0s|w|WT|W';
- var reg = new RegExp(str, 'g');
- format = format.replace(reg, (mstr)=>{
- switch(mstr){
- case 'yyyy': return this.y;
- case 'YYYY': return this.perNumToChinese(this.y);
- case 'yy': return ('' + this.y).slice(-2);
- case 'YY': return this.perNumToChinese( +('' + this.y).slice(-2) );
- case 'mm': return this.m;
- case 'MM': return this.numToChinese(this.m);
- case '0m': return this.m < 10 ? '0'+ this.m : this.m;
- case 'dd': return this.d;
- case 'DD': return this.numToChinese(this.d);
- case '0d': return this.d < 10 ? '0' + this.d : this.d;
- case 'hh': return this.h;
- case 'HH': return this.numToChinese(this.h);
- case '0h': return this.h < 10 ? '0' + this.h : this.h;
- case 'ff': return this.f;
- case 'FF': return this.numToChinese(this.f);
- case '0F': return this.f<10&&this.f>0 ? ('零' + this.numToChinese(this.f) ):this.numToChinese(this.f);
- case '0f': return this.f < 10 ? '0' + this.f : this.f;
- case 'ss': return this.s;
- case 'SS': return this.numToChinese(this.s);
- case '0S': return this.s<10&&this.s>0 ? ('零' + this.numToChinese(this.s) ):this.numToChinese(this.s);
- case '0s': return this.s < 10 ? '0' + this.s : this.s;
- case 'w': return this.w;
- case 'W': return this.w == 7 ? '日' : this.numToChinese(this.w);
- case 'WT': return this.w == 7 ? '天' : this.numToChinese(this.w);
- }
- });
- return format;
- }
- __analysis(str, rule){
- let resultObj;
- if(typeof rule === 'function'){
- resultObj = rule(str);
- if(!resultObj){
- return null;
- }
- }else{
- resultObj = {};
- var str11 =
- 'y{4}|Y{4}|y{2}|Y{2}|mm|MM|0m|dd|DD|0d|hh|HH|0h|ff|FF|0F|0f|ss|SS|0S|0s|w|WT|W';
- var reg = new RegExp(str11, 'g');
-
- var namesArr = [null];
- rule = rule.replace(reg, (mstr)=>{
- var result = this.__ANALYREGSTR[mstr];
- if(result){
- namesArr.push(mstr);
- return `(${result})`;
- }
- });
-
- var resultArr = str.match( new RegExp(rule) );
- if(resultArr){
- for(let i=1,val; i<namesArr.length; i++){
- val = resultArr[i];
- if(val == null){
- continue;
- }
- switch(namesArr[i]){
- case 'yyyy': resultObj.y = +val; break;
- case 'YYYY': resultObj.y = this.perChineseToNum(val); break;
- case 'yy': resultObj.y = +val + 2000; break;
- case 'YY': resultObj.y = this.perChineseToNum(val) + 2000; break;
- case 'mm': resultObj.m = +val; break;
- case 'MM': resultObj.m = this.chineseToNum(val); break;
- case '0m': resultObj.m = +val; break;
- case 'dd': resultObj.d = +val; break;
- case 'DD': resultObj.d = this.chineseToNum(val); break;
- case '0d': resultObj.d = +val; break;
- case 'hh': resultObj.h = +val; break;
- case 'HH': resultObj.h = this.chineseToNum(val); break;
- case '0h': resultObj.h = +val; break;
- case 'ff': resultObj.f = +val; break;
- case 'FF': resultObj.f = this.chineseToNum(val); break;
- case '0F': resultObj.f = this.chineseToNum(val); break;
- case '0f': resultObj.f = +val; break;
- case 'ss': resultObj.s = +val; break;
- case 'SS': resultObj.s = this.chineseToNum(val); break;
- case '0S': resultObj.s = this.chineseToNum(val); break;
- case '0s': resultObj.s = +val; break;
- case 'w': resultObj.w = 7; break;
- case 'W': resultObj.w = 7; break;
- case 'WT': resultObj.w = 7; break;
- }
- }
- }else{
- return null;
- }
-
- }
-
- return resultObj;
- }
- __ANALYREGSTR = {
- 'yyyy': '\\d{4}',
- 'YYYY': '[零一二三四五六七八九]{4}',
- 'yy': '\\d{2}',
- 'YY': '[零一二三四五六七八九]{2}',
- 'mm': '[1-9]|1[012]',
- 'MM': '十[一二]?|[一二三四五六七八九]',
- '0m': '[0][1-9]|1[012]',
- 'dd': '3[01]|[12]0|[12]?[1-9]',
- 'DD': '三十[一]?|二?十[一二三四五六七八九]|[零一二三四五六七八九十]',
- '0d': '[012][0-9]|3[01]',
- 'hh': '2[0-4]|[1]?[0-9]',
- 'HH': '二十[一二三]?|十[一二三四五六七八九]|[零一二三四五六七八九十]',
- '0h': '[01][0-9]|2[0-4]',
- 'ff': '60|[1-5]?[0-9]',
- 'FF': '[二三四五]十[一二三四五六七八九]|十[一二三四五六七八九]|[二三四五六]十|[零一二三四五六七八九十]',
- '0F': '零[零一二三四五六七八九]|十[一二三四五六七八九]|[二三四五]十[一二三四五六七八九]|[二三四五六]十|十',
- '0f': '[0-5][0-9]|60',
- 'ss': '60|[1-5]?[0-9]',
- 'SS': '[二三四五]十[一二三四五六七八九]|十[一二三四五六七八九]|[二三四五六]十|[零一二三四五六七八九十]',
- '0S': '零[零一二三四五六七八九]|十[一二三四五六七八九]|[二三四五]十[一二三四五六七八九]|[二三四五六]十|十',
- '0s': '[0-5][0-9]|60',
- 'w': '7',
- 'W': '日',
- 'WT': '天',
- }
-
- fillChar(num, len, char){
- if(typeof num === 'number'){
- char = char || '0';
- }else{
- char = char || ' ';
- }
- let str = '' + num, charStr = '';
- if(str.length < len){
- for(let i=0, len1=len-str.length; i<len1; i++){
- charStr += char;
- }
- return charStr + str;
- }
- return str;
- }
- __SIZEPOW = {'十': 1, '百': 2, '千': 3, '万': 4, '亿': 8};
-
- chineseToNum (numStr){
- if(numStr.length === 1){
- return this.perChineseToNum(numStr);
- }
- if(numStr.length === 2 && numStr[0] === '十'){
- return this.perChineseToNum(numStr[1]) + 10;
- }
- let arr;
- if(!numStr.match(/[亿万]/)){
- arr = numStr.split('零');
- let num = 0, val = 0;
- for(let i=0, str=''; i<arr.length; i++){
- str = arr[i];
- for(let j=0; j<str.length; j++){
- if(str[j].match(/[一二三四五六七八九]/)){
- num += val;
- val = 1;
- val *= this.perChineseToNum(str[j]);
- }else{
- val *= Math.pow(10, this.__SIZEPOW[str[j]] )
- }
- }
- }
- num += val;
- return num;
- }else{
- arr = numStr.split('亿');
- if(arr.length > 1){
- var arr1 = arr[1].split('万');
- return this.chineseToNum(arr[0])*Math.pow(10, 8) + this.chineseToNum(arr1[0])*10000 + this.chineseToNum(arr1[1]);
- }else{
- return this.chineseToNum(arr[0])*Math.pow(10, 8) + this.chineseToNum(arr[1]);
- }
- }
- }
-
- perChineseToNum(numStr){
- return +numStr.replace(/[零一二三四五六七八九十\s]/g, (str)=>{
- var index = this.__CHINANUM.indexOf(str);
- return index == null ? '' : index;
- });
- }
-
- numToChinese (num){
- let numStr = '' + num;
- let len = numStr.length, result;
- if(num <= 10){
- return this.__CHINANUM[num];
- }else if(num < 20){
- return '十' + this.__CHINANUM[numStr[1]];
- }else if(len < 6){
- if(!this.__WEIARR){
- this.__WEIARR = this.__WEISTR.split(', ');
- }
- let lastStr, resultStr = '', index;
- for(let i=0; i<len; i++){
- lastStr = this.__CHINANUM[numStr[i]];
- if(numStr[i] === '0'){
- index = null;
- for(let j=i+1; j<len; j++){
- if( numStr[j] !== '0' ){
- index = j - 1;
- break;
- }
- }
- if(index === null){
- break;
- }else{
- i = index;
- resultStr += '零';
- }
- }else{
- resultStr += ( lastStr + this.__WEIARR[len - i - 1] );
- }
- }
- return resultStr;
- }else if(len < 9){
- let low4Str = numStr.slice(-4), heigh4Str = numStr.slice(0, -4), isZero = '';
- isZero = heigh4Str.slice(-1) === '0' ? '零' : '';
- if(isZero){
- low4Str = +low4Str;
- if(low4Str === 0){
- isZero = '';
- }
- }
- if(+heigh4Str !== 0){
- isZero = '万' + isZero;
- }
- result = this.numToChinese( +heigh4Str ) + isZero + this.numToChinese( +low4Str );
- return result.slice(-1) === '零' ? result.slice(0, -1) : result;
- }else if(len < 14){
- let low8Str = numStr.slice(-8), heigh4Str = numStr.slice(0, -8), isZero = '';
- isZero = heigh4Str.slice(-1) === '0' ? '零' : '';
- if(isZero){
- low8Str = +low8Str;
- if(low8Str === 0){
- isZero = '';
- }
- }
- if(+heigh4Str !== 0){
- isZero = '亿' + isZero;
- }
- result = this.numToChinese( +heigh4Str ) + isZero + this.numToChinese( +low8Str );
- return result.slice(-1) === '零' ? result.slice(0, -1) : result;
- }
- }
-
- perNumToChinese(num){
- return ('' + num).replace(/[\d\s]/g, (index)=>{
- var result = this.__CHINANUM[index];
- return result ? result : '';
- });
- }
- __CHINANUM = '零一二三四五六七八九十';
- __WEISTR =', 十, 百, 千, 万, 十万, 百万, 千万, 亿, 十亿, 百亿, 千亿, 万亿, 十万亿, 百万亿, 千万亿, 亿亿, 十亿亿, 百亿亿, 千亿亿, 万亿亿';
- __WEIARR = null;
-
- }
|