3. Date processing function
Date processing is also an important aspect of js, and it is more likely to cause unexpected errors or bugs. Here are some common functions that need to be paid attention to when collecting and sorting (encapsulated into a time class):
// Date object
function PowerDate() {
this .date = null ;
// Whether to add zero replacement flag during formatting
this .isFmtZero = false ;
this .weekArr = [[ "Sunday ", "Monday ", "Tuesday ", "Thursday ", "Friday ", "Saturday "], ],
[ " SUN " , " MON " , " TUR " , " WED " , " THU " , " FRI " , " SAT " ]];
this .monthArr = [ " JAN " , " FEB " , " MAR " , " APR " , " MAY " , " JUN " , " JUL " , " AUG " , " SEP " , " OCT " , " NOV " , " DEC " ];
// Initialize date object
switch () {
case 0 :
this .date = new Date();
break ;
case 1 :
var reg = / ^(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})$ / ;
var str = arguments[ 0 ].replace( / \s / , "" );
str = (reg, " $1/$2/$3 " );
this .date = new Date(str);
break ;
case 3 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ]);
break ;
case 6 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ], arguments[ 3 ], arguments[ 4 ], arguments[ 5 ]);
break ;
case 7 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ], arguments[ 3 ], arguments[ 4 ], arguments[ 5 ], arguments[ 6 ]);
break ;
default : this .date = new Date( " 1970/1/1 " ); break ;
}
// Initialization failure handling
if ( typeof ( this .date) != " object " || ! ( / Date / .test( this .)))
throw ( new Error( - 1 , ' Failed to construct the PowerDate method, check the input parameters! ' ));
this .getDate = function () {
return this .date;
}
this .getFullYear = function () {
return this .();
};
this .getYear = function () {
return this .();
};
this .getMonth = function () {
return this .frmWithZero( this .() + 1 );
};
this .getDay = function () {
return this .frmWithZero( this .());
};
this .getHour = function () {
return this .frmWithZero( this .());
};
this .getMinute = function () {
return this .frmWithZero( this .());
};
this .getSecond = function () {
return this .frmWithZero( this .());
};
this .getMillisecond = function () {
var ss = this .();
if ( this .isFmtZero == true && ss < 10 )
return " 00 " + ss;
else if ( this .isFmtZero == true && ss < 100 )
return " 0 " + ss;
else return ss;
};
this .getWeek = function () {
return this .();
};
this .setIsFmtZero = function (val) {
this .isFmtZero = val;
};
this .frmWithZero = function (num) {
if ( this .isFmtZero == true && num < 10 )
return " 0 " + num;
else return num;
}
/*
Function: Return date string according to input expression
Parameters: dateFmt: string, consisting of the following structures: yy: long write year, YY: short write year mm: number month, MM: English month, dd: day, hh: hour, mi: minute, ss seconds, ms: milliseconds, we: Chinese character week, WE: English week.
isFmtZero: Boolean value, true: 0 needs to be filled, false: 0 does not need to be filled
*/
this .getString = function (dateFmt) {
if ( typeof (dateFmt) != " string " )
throw ( new Error( - 1 , ' getString() method requires string type parameter! ' ));
var str = dateFmt;
str = ( / yy / g, this .getFullYear());
str = ( / YY / g, this .getYear());
str = ( / mm / g, this .getMonth());
str = ( / MM / g, this .monthArr[ this .getMonth() - 1 ]);
str = ( / dd / g, this .getDay());
str = ( / hh / g, this .getHour());
str = ( / mi / g, this .getMinute());
str = ( / ss / g, this .getSecond());
str = ( / ms / g, this .getMillisecond());
str = ( / we / g, this .weekArr[ 0 ][ this .getWeek()]);
str = ( / WE / g, this .weekArr[ 1 ][ this .getWeek()]);
return str;
};
/* Function: Returns the date N days (N 24 hours) away from a date
* Parameter: number type can be positive and negative integer or floating point number
* Return : New PowerDate type
* Method: (num);
*/
this .dateAfterDays = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num) parameter is numeric type. " );
var dd = this .();
dd += num * 24 * 3600 * 1000 ;
this .date = new Date(dd);
return this ;
};
/* Function: Returns the date N seconds away from a date
* Parameter: number type can be positive and negative integer or floating point number
* Return : New date
* Method: (num);
*/
this .dateAfterSeconds = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num) parameter is numeric type. " );
var dd = this .();
dd += num * 1000 ;
this .date = new Date(dd);
return this ;
};
// Determine whether it is a leap year
this .isLeapYear = function () {
var year = this .getFullYear();
return ( 0 == year % 4 && ((year % 100 != 0 ) || (year % 400 == 0 )));
};
// Return the number of days in that month
this .getDaysOfMonth = function () {
return ( new Date( this .getFullYear(), this .getMonth(), 0 )).getDate();
};
// Convert to capital date (Chinese)
this .getChinaDate = function () {
var year = this .getFullYear();
var month = this .getMonth();
var day = this .getDay();
var arrNum = [ "zero " , "one " , "two " , "three " , "four " , "five " , "six " , "seven " , "eight " , "nine " , "ten " , "eleven " , "twelve " ] ];
var strTmp = "" ;
for ( var i = 0 , j = ().length; i < j; i ++ ) {
strTmp += arrNum[().charAt(i)];
}
strTmp += "year " ;
if (().substr( 0 , 1 ) == 0 ) {
strTmp += arrNum[parseInt(().substr( 1 ), 10 )] + "month " ;
}
else
strTmp += arrNum[month] + "month " ;
if (day < 10 )
strTmp += arrNum[parseInt(().substr( 1 ), 10 )];
else if (day < 20 )
strTmp += " ten " + arrNum[day - 10 ];
else if (day < 30 )
strTmp += " Twenty " + arrNum[day - 20 ];
else
strTmp += " Thirty " + arrNum[day - 30 ];
strTmp += "Day " ;
return strTmp;
};
// Date comparison function, such as greater than parameter: 1, equality: 0, inequality: -1
this .dateCompare = function (dat) {
if ( typeof (dat) == " string " ) {
if (dat != "" ) dat = new Date(timeString);
else dat = new Date();
}
if ( typeof (dat) != " object " || ! ( / Date / .test( this .))) {
throw new Error( - 2 , " The parameter of dateCompare is date type or can be directly converted into a string of date type!" );
}
var d = this .() - ();
return d > 0 ? 1 : (d == 0 ? 0 : - 1 );
};
/* Function: Return the difference between two dates
* Parameters:pd PowerDate object
* type: Returns the category identification.yy: year,mm: month,dd: day,hh: hour,mi: minute,ss: seconds,ms: milliseconds
* intOrFloat: Returns integer or floating point value 0: integer, 1: floating point
*/
this .calDateDistance = function (pd, type, intOrFloat) {
var miSecMain = this .();
var miSecSub = ().valueOf();
var num = 0 ;
switch (type) {
case " yy " : num = this .getFullYear() - ();
break ;
case " mm " : num = ( this .getFullYear() - ()) * 12 + this .getMonth() - ();
break ;
case " dd " : num = this .fmtRtnVal((miSecMain - miSecSub) / 86400000 , intOrFloat);
break ;
case " hh " : num = this .fmtRtnVal((miSecMain - miSecSub) / 3600000 , intOrFloat);
break ;
case " mi " : num = this .fmtRtnVal((miSecMain - miSecSub) / 60000 , intOrFloat);
break ;
case " ss " : num = this .fmtRtnVal((miSecMain - miSecSub) / 1000 , intOrFloat);
break ;
case " ms " : num = (miSecMain - miSecSub);
break ;
default :
throw new Error( - 1 , "There is no type you requested to return, please check the input parameters!" );
break ;
}
return num;
};
this .fmtRtnVal = function (val, intOrFloat) {
// alert(val);
return (intOrFloat == 0 ? (val) : parseInt(val * 100 ) / 100 );
};
}
// test
function test() {
var d = new PowerDate( " 1998/7/3 " ); // Instantiate a PowerDate object
( true ); // Set to fill the output with 0
alert(( " yy-mm-dd hh:mi: we " )); // Output date string
var d2 = new PowerDate(); // Instantiate a PowerDate object
alert(( new PowerDate( " 2005/7/31 " ), " yy " , 1 )); // Output date string
alert(());
alert(( 3 ).getFullYear());
}