JS to obtain the current date and time and other operations 2008-07-28-06-11 17: 24var 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 () % 100! = 0) | (this. getyear () % 400 = 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;
}
// Calculate the date of the previous day of the current date
VaR mydate = new date ();
VaR edate = mydate. tolocaledatestring ();
VaR year = mydate. getfullyear ();
VaR month = mydate. getmonth () + 1;
VaR day = mydate. getdate ();
VaR edate = year + "-" + month + "-" + day; // end date
VaR UOM = new date (year, month, day );
UOM. setdate (UOM. getdate ()-1 );
VaR smonth = UOM. getmonth ();
Alert (smonth)
VaR SDAY = UOM. getdate ();
VaR syear = UOM. getfullyear ();
If (Day = 1 & month = 1)
{
Smonth = 12;
Syear --;
SDAY = getdaysinmonth (syear, smonth );
}
Else if (Day = 1)
{
SDAY = getdaysinmonth (syear, smonth );
}
VaR sdate = syear + "-" + smonth + "-" + SDAY; // start date
$ ("# Starttime"). Val (sdate );
$ ("# Endtime"). Val (edate );
// Obtain the number of days in a month
Function getdaysinmonth (year, month)
{
Month = parseint (month, 10) + 1;
VaR temp = new date (year + "/" + month + "/0 ");
Return temp. getdate ();
}
// Calculate the number of days between two dates
Function datediff (sdate1, sdate2)
{
VaR adate, odate1, odate2, idays;
Adate = sdate1.split ("-");
Odate1 = new date (adate [1] + '-' + adate [2] + '-' + adate [0]); // convert to the 12-18-2002 format
Adate = sdate2.split ("-");
Odate2 = new date (adate [1] + '-' + adate [2] + '-' + adate [0]);
Idays = parseint (math. Abs (odate1-odate2)/1000/60/60/24); // converts the number of milliseconds to the number of days
Return idays;
}
// Obtain the number of days in a month
Function getdaysinmonth (year, month)
{
Month = parseint (month, 10) + 1;
VaR temp = new date (year + "/" + month + "/0 ");
Return temp. getdate ();
}
// Subtraction
Function Sub (arg1, arg2 ){
VaR R1, R2, M, N;
Try {R1 = arg1.tostring (). Split (".") [1]. Length} catch (e) {R1 = 0}
Try {r2 = arg2.tostring (). Split (".") [1]. Length} catch (e) {r2 = 0}
M = math. Pow (10, math. Max (R1, R2 ));
// Dynamically control the Precision Length
N = (r1> = R2 )? R1: R2;
Return parsefloat (arg1 * m-arg2 * m)/m). tofixed (n ));
}