<Script language = "JavaScript">
VaR d = new date ()
VaR vyear = D. getfullyear ()
VaR vmon = D. getmonth () + 1
VaR vday = D. getdate ()
VaR H = D. gethours ();
VaR M = D. getminutes ();
VaR Se = D. getseconds ();
S = vyear + (vmon <10? "0" + vmon: vmon) + (vday <10? "0" + vday: vday) + (H <10? "0" + H: H) + (M <10? "0" + M: m) + (SE <10? "0" + Se: SE); document. Write (s); // output time </SCRIPT>
VaR mydate = new date ();
Mydate. getyear (); // obtain the current year (2 digits)
Mydate. getfullyear (); // obtain the complete year (4 bits, 1970 -????)
Mydate. getmonth (); // obtain the current month (0-11, 0 represents January)
Mydate. getdate (); // obtain the current day (1-31)
Mydate. getday (); // obtain X of the current week (0-6, 0 indicates Sunday)
Mydate. gettime (); // obtain the current time (milliseconds starting from 1970.1.1)
Mydate. gethours (); // obtain the current hour (0-23)
Mydate. getminutes (); // obtain the current number of minutes (0-59)
Mydate. getseconds (); // obtain the current number of seconds (0-59)
Mydate. getmilliseconds (); // obtain the current number of milliseconds (0-999)
Mydate. tolocaledatestring (); // obtain the current date
VaR mytime = mydate. tolocaletimestring (); // obtain the current time
Mydate. tolocalestring (); // obtain the date and time
Date and Time script library method list
Date. Prototype. isleapyear
Date. Prototype. format
Date. Prototype. dateadd date calculation
Date. Prototype. datediff compare date difference
Date. Prototype. tostring date to string
Date. Prototype. toarray date is split into Arrays
Date. Prototype. datepart obtains part of the date information.
Date. Prototype. maxdayofdate indicates the maximum number of days in the month where the date is located.
Date. Prototype. weeknumofyear determine the week of the year where the date is located
Stringtodate string to date type
Isvaliddate verification date validity
Checkdatetime full Date and Time check
Daysbetween date day difference
JS Code:
//---------------------------------------------------
// Determine the leap year
//---------------------------------------------------
Date. Prototype. isleapyear = function ()
{
Return (0 = This. getyear () % 4 & (this. getyear () 0! = 0) | (this. getyear () @ 0 = 0 )));
}
//---------------------------------------------------
// Format the date
// Format: yyyy/YYYY/yy indicates the year
// Mm/M month
// W/w weeks
// DD/D/d Date
// Hh/h time
// Mm/M minutes
// SS/S seconds
//---------------------------------------------------
Date. Prototype. format = function (formatstr)
{
VaR STR = formatstr;
VaR week = ['day', 'yi', '2', '3', '4', '5', '6'];
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 ()> 9? This. getmonth (). tostring (): '0' + this. getmonth ());
STR = Str. Replace (/M/G, this. getmonth ());
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;
}
// + ---------------------------------------------------
// | Returns the number of days difference between two time ranges in the format of yyyy-mm-dd.
// + ---------------------------------------------------
Function daysbetween (dateone, datetwo)
{
VaR onemonth = dateone. substring (5, dateone. lastindexof ('-'));
VaR oneday = dateone. substring (dateone. length, dateone. lastindexof ('-') + 1 );
VaR oneyear = dateone. substring (0, dateone. indexof ('-'));
VaR twomonth = datetwo. substring (5, datetwo. lastindexof ('-'));
VaR twoday = datetwo. substring (datetwo. length, datetwo. lastindexof ('-') + 1 );
VaR twoyear = datetwo. substring (0, datetwo. indexof ('-'));
VaR CHA = (date. parse (onemonth + '/' + oneday + '/' + oneyear)-date. parse (twomonth + '/' + twoday + '/' + twoyear)/86400000 );
Return math. Abs (CHA );
}
// + ---------------------------------------------------
// | Date calculation
// + ---------------------------------------------------
Date. Prototype. dateadd = function (strinterval, number ){
VaR dttmp = this;
Switch (strinterval ){
Case's ': return new date (date. parse (dttmp) + (1000 * number ));
Case 'N': return new date (date. parse (dttmp) + (60000 * number ));
Case 'H': return new date (date. parse (dttmp) + (3600000 * number ));
Case 'D': return new date (date. parse (dttmp) + (86400000 * number ));
Case 'W': return new date (date. parse (dttmp) + (86400000*7) * number ));
Case 'q': return new date (dttmp. getfullyear (), (dttmp. getmonth () + number * 3, dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), dttmp. getseconds ());
Case 'M': return new date (dttmp. getfullyear (), (dttmp. getmonth () + number, dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), dttmp. getseconds ());
Case 'y': return new date (dttmp. getfullyear () + number), dttmp. getmonth (), dttmp. getdate (), dttmp. gethours (), dttmp. getminutes (), dttmp. getseconds ());
}
}
// + ---------------------------------------------------
// | Compare date difference dtend format is date type or valid Date Format String
// + ---------------------------------------------------
Date. Prototype. datediff = function (strinterval, dtend ){
VaR dtstart = this;
If (typeof dtend = 'string') // If the string is converted to the date type
{
Dtend = stringtodate (dtend );
}
Switch (strinterval ){
Case's ': Return parseint (dtend-dtstart)/1000 );
Case 'N': Return parseint (dtend-dtstart)/60000 );
Case 'H': Return parseint (dtend-dtstart)/3600000 );
Case 'D': Return parseint (dtend-dtstart)/86400000 );
Case 'W': Return parseint (dtend-dtstart)/(86400000*7 ));
Case 'M': Return (dtend. getmonth () + 1) + (dtend. getfullyear ()-dtstart. getfullyear () * 12)-(dtstart. getmonth () + 1 );
Case 'y': Return dtend. getfullyear ()-dtstart. getfullyear ();
}
}
// + ---------------------------------------------------
// | Date output string, which reloads the system's tostring Method
// + ---------------------------------------------------
Date. Prototype. tostring = function (showweek)
{
VaR mydate = this;
VaR STR = mydate. tolocaledatestring ();
If (showweek)
{
VaR week = ['day', 'yi', '2', '3', '4', '5', '6'];
STR + = 'Week' + week [mydate. getday ()];
}
Return STR;
}
// + ---------------------------------------------------
// | Date validity Verification
// | Format: YYYY-MM-DD or yyyy/mm/dd
// + ---------------------------------------------------
Function isvaliddate (datestr)
{
VaR sdate = datestr. Replace (/(^ \ s + | \ s + $)/g, ''); // remove spaces on both sides;
If (sdate = '') return true;
// If the format is yyyy-(/) Mm-(/) dd Or yyyy-(/) m -(/) D or yyyy-(/) Mm-(/) D is replaced''
// In the database, the valid date can be: YYYY-MM/DD (2003-3/21), the database will automatically convert to the YYYY-MM-DD format
VaR S = sdate. replace (/[\ D] {4, 4} [\-/] {1} [\ D] {1} [\-/] {1} [\ D] {1, 2} /g, '');
If (S = '') // The description format meets the YYYY-MM-DD or YYYY-M-DD or YYYY-M-D or YYYY-MM-D
{
VaR T = new date (sdate. Replace (/\-/g ,'/'));
VaR AR = sdate. Split (/[-/:]/);
If (AR [0]! = T. getyear () | ar [1]! = T. getmonth () + 1 | ar [2]! = T. getdate ())
{
// Alert ('incorrect Date Format! Format: YYYY-MM-DD or yyyy/mm/DD. Pay attention to the leap year. ');
Return false;
}
}
Else
{
// Alert ('incorrect Date Format! Format: YYYY-MM-DD or yyyy/mm/DD. Pay attention to the leap year. ');
Return false;
}
Return true;
}
// + ---------------------------------------------------
// | Date and Time check
// | Format: YYYY-MM-DD hh: mm: SS
// + ---------------------------------------------------
Function checkdatetime (STR)
{
VaR Reg =/^ (\ D +)-(\ D {1, 2})-(\ D {1, 2}) (\ D {1, 2}) :( \ D {1, 2 }) :( \ D {1, 2}) $ /;
VaR r = Str. Match (REG );
If (r = NULL) return false;
R [2] = R [2]-1;
VaR d = new date (R [1], R [2], R [3], R [4], R [5], R [6]);
If (D. getfullyear ()! = R [1]) return false;
If (D. getmonth ()! = R [2]) return false;
If (D. getdate ()! = R [3]) return false;
If (D. gethours ()! = R [4]) return false;
If (D. getminutes ()! = R [5]) return false;
If (D. getseconds ()! = R [6]) return false;
Return true;
}
// + ---------------------------------------------------
// | Split the date into an array
// + ---------------------------------------------------
Date. Prototype. toarray = function ()
{
VaR mydate = this;
VaR myarray = array ();
Myarray [0] = mydate. getfullyear ();
Myarray [1] = mydate. getmonth ();
Myarray [2] = mydate. getdate ();
Myarray [3] = mydate. gethours ();
Myarray [4] = mydate. getminutes ();
Myarray [5] = mydate. getseconds ();
Return myarray;
}
// + ---------------------------------------------------
// | Obtain date data
// | Interval indicates the data type.
// | Y, M, D, W, week WW, week h, n minutes, S
// + ---------------------------------------------------
Date. Prototype. datepart = function (interval)
{
VaR mydate = this;
VaR partstr = '';
VaR week = ['day', 'yi', '2', '3', '4', '5', '6'];
Switch (interval)
{
Case 'y': partstr = mydate. getfullyear (); break;
Case 'M': partstr = mydate. getmonth () + 1; break;
Case 'D': partstr = mydate. getdate (); break;
Case 'W': partstr = week [mydate. getday ()]; break;
Case 'ww ': partstr = mydate. weeknumofyear (); break;
Case 'H': partstr = mydate. gethours (); break;
Case 'N': partstr = mydate. getminutes (); break;
Case's ': partstr = mydate. getseconds (); break;
}
Return partstr;
}
// + ---------------------------------------------------
// | Obtain the maximum number of days in the month of the current date
// + ---------------------------------------------------
Date. Prototype. maxdayofdate = function ()
{
VaR mydate = this;
VaR ary = mydate. toarray ();
VaR date1 = (new date (ary [0], Ary [1] + 1, 1 ));
VaR date2 = date1.dateadd (1, 'M', 1 );
VaR result = datediff (date1.format ('yyyy-mm-dd'), date2.format ('yyyy-mm-dd '));
Return result;
}
// + ---------------------------------------------------
// | The week in which the current date is obtained is the week of the year.
// + ---------------------------------------------------
Date. Prototype. weeknumofyear = function ()
{
VaR mydate = this;
VaR ary = mydate. toarray ();
VaR year = ary [0];
VaR month = ary [1] + 1;
VaR day = ary [2];
Document. Write ('<script language = VBScript \> \ n ');
Document. Write ('mydate = datevalue (''+ month + '-' + day + '-' + year +'') \ n ');
Document. Write ('result = datepart ('ww ', mydate) \ n ');
Document. Write ('\ n ');
Return result;
}
// + ---------------------------------------------------
// | Convert string to date type
// | Mm/DD/YYYY mm-dd-yyyy/mm/DD yyyy-mm-dd
// + ---------------------------------------------------
Function stringtodate (datestr)
{
VaR converted = date. parse (datestr );
VaR mydate = new date (converted );
If (isnan (mydate ))
{
// Var delimcahar = datestr. indexof ('/')! =-1? '/':'-';
VaR arys = datestr. Split ('-');
Mydate = new date (arys [0], -- arys [1], arys [2]);
}
Return mydate;
}