Javascript date processing functions 1. Solve 2000 problems Function Y2K (number) {return (number <1000 )? Number + 1900: Number ;} 2. Check whether the date is valid // When the input parameter is isdate (DD, mm, ccyy), it indicates the year, month, and day to be checked. // When the input parameter is isdate (DD, mm), the default year is the current year. // When the input parameter is isdate (dd), it indicates the default year, and the month indicates the current year. // Ensure that the input month is less than 1-12. Function isdate (day, month, year ){ VaR today = new date (); Year = ((! Year )? Y2K (today. getyear () year ); Month = ((! Month )? Today. getmonth (): month-1 ); If (! Day) return false VaR test = new date (year, month, day ); If (Y2K (test. getyear () = year )&& (Month = test. getmonth ())&& (Day = test. getdate ())) Return true; Else Return false } The following is a call example: If (isdate (1997 )) Document. Write ("valid "); Else Document. Write ("invalid "); 3. How to determine the number of days between two dates function dayselapsed (date1, date2) { var Difference = date. UTC (date1.getyear (), date1.getmonth (), date1.getdate (), 0, 0) -date. UTC (date2.getyear (), date2.getmonth (), date2.getdate (), 1000/60, 0); Return Difference/60/24/; } 4. How to pass the month in a drop-down list box to another page <Form> <Select name = "selectname"> <Option> January <Option> February <Option> March <Option> CMDL <Option> May <Option> June <Option> July <Option> August <Option> spetember <Option> October <Option> November <Option> December </SELECT> <Input type = "button" value = "go" onclick = "window. Location. href = 'nextpage.html? '+ This. Form. selectname. Options [This. Form. selectname. selectedindex]. Text "> </Form> Add the followingCode <Form name = "formname"> <input type = "text" name = "textname"> <form> <Script language = "JavaScript"> < Document. formname. textname. value = location. Search. substring (1 ); //-Script> Or: <Script language = "JavaScript"> < Document. Write ("<form> <input type = 'text '"); Document. Write ("value = '" location. Search. substring (1) + "form> ") //-Script> 5. How to compare the time in a string with the current time <Script language = "JavaScript"> /* The date string can be in the following format: Format: 19970529 Format: 970529 Format 3: 29/05/1997 Format 4: 29/05/97 The input parameter datetype is a number ranging from 1 to 4, indicating the format used. */ <! - Function isittoday (datestring, datetype ){ VaR now = new date (); VaR today = new date (now. getyear (), now. getmonth (), now. getdate ()) If (datetype = 1) VaR date = new date (datestring. substring (0, 4 ), Datestring. substring (4, 6)-1, Datestring. substring (6, 8 )); Else if (datetype = 2) VaR date = new date (datestring. substring (0, 2 ), Datestring. substring (2, 4)-1, Datestring. substring (4, 6 )); Else if (datetype = 3) VaR date = new date (datestring. substring (6, 10 ), Datestring. substring (3,5)-1, Datestring. substring (0, 2 )); Else if (datetype = 4) VaR date = new date (datestring. substring (6, 8 ), Datestring. substring (3,5)-1, Datestring. substring (0, 2 )); Else Return false; If (date. tostring () = today. tostring () return true; else return false; }< br> the call example is as follows: If (isittoday ("19970529", 1) Alert ('true'); else alert ('false'); If (isittoday ("970529 ", 2) Alert ('true'); else alert ('false'); If (isittoday ("29/05/1997", 3) Alert ('true '); else alert ('false'); If (isittoday ("02/06/97", 4) Alert ('true'); else alert ('false '); //- 6. How to calculate the age based on a person's birthday <Script language = "JavaScript"> < /* The date string can be in the following format: Format: 19970529 Format: 970529 Format 3: 29/05/1997 Format 4: 29/05/97 The input parameter datetype is a number ranging from 1 to 4, indicating the format used. */ Function getage (datestring, datetype ){ VaR now = new date (); VaR today = new date (now. getyear (), now. getmonth (), now. getdate ()) VaR yearnow = now. getyear (); VaR monthnow = now. getmonth (); VaR datenow = now. getdate (); If (datetype = 1) VaR DOB = new date (datestring. substring (0, 4 ), Datestring. substring (4, 6)-1, Datestring. substring (6, 8 )); Else if (datetype = 2) VaR DOB = new date (datestring. substring (0, 2 ), Datestring. substring (2, 4)-1, Datestring. substring (4, 6 )); Else if (datetype = 3) VaR DOB = new date (datestring. substring (6, 10 ), Datestring. substring (3,5)-1, Datestring. substring (0, 2 )); Else if (datetype = 4) VaR DOB = new date (datestring. substring (6, 8 ), Datestring. substring (3,5)-1, Datestring. substring (0, 2 )); Else Return ''; VaR yeardob = Dob. getyear (); VaR monthdob = Dob. getmonth (); VaR datedob = Dob. getdate (); Yearage = yearnow-yeardob; If (monthnow> monthdob) var monthage = monthnow-monthdob; else { yearage --; var monthage = 12 + monthnow-monthdob; }< br> If (datenow> datedob) var dateage = datenow-datedob; else { monthage --; var dateage = 31 + datenow-datedob; } Return yearage + 'ears' + monthage + 'months' + dateage + 'days '; } Call example Document. Write (getage ("19650104", 1) + 'br> ') Document. Write (getage ("650104", 2) + 'br> ') Document. Write (getage ("04/01/1965", 3) + 'br> ') Document. Write (getage ("04/01/65", 4) + 'br> ') //-Script> 7. How to use the following format dd/mm/yy to display the date on the webpage <Script language = 'javascript '> <! - VaR date = new date (); VaR d = date. getdate (); VaR day = (d <10 )? '0' + D: D; VaR M = date. getmonth () + 1; VaR month = (M <10 )? '0' + M: m; VaR YY = date. getyear (); VaR year = (yy <1000 )? YY + 1900: yy; Document. Write (day + "/" + month + "/" + year ); //- </SCRIPT> 8. How to use the following format date month year to display a date on a webpage <Script language = 'javascript '> <! - Function makearray (){ For (I = 0; I <makearray. Arguments. length; I ++) This [I + 1] = makearray. Arguments [I]; } VaR months = new makearray ('january ', 'february', 'march ', '0000l', 'may', 'june', 'july', 'August ', 'September ', 'October ', 'november', 'december '); VaR date = new date (); VaR day = date. getdate (); VaR month = date. getmonth () + 1; VaR YY = date. getyear (); VaR year = (yy <1000 )? YY + 1900: yy; Document. Write (day + "" + months [month] + "" + year ); //- </SCRIPT> [Page] 9. How to make the last update date of my webpage easier to read <Script language = 'javascript '> < Function makearray0 (){ For (I = 0; I <makearray0.arguments. length; I ++) This [I] = makearray0.arguments [I]; } VaR days = new makearray0 ("Sunday", "Monday", "Tuesday", "Wednesday ", "Thursday", "Friday", "Saturday "); VaR months = new makearray0 ('january ', 'february', 'march ', '0000l', 'may', 'june', 'july', 'August ', 'September ', 'October ', 'november', 'december '); Function nths (day ){ If (Day = 1 | day = 21 | day = 31) Return 'st '; Else If (Day = 2 | day = 22) Return 'nd '; If (Day = 3 | day = 23) Return 'RD '; Else return 'th '; } Function Y2K (number) {return (number <1000 )? Number + 1900: Number ;} VaR last = Document. lastmodified; VaR date = new date (last ); Document. Write ("Last updated on" + days [date. getday ()] + ''+ Date. getdate () + nths (date. getdate () + "" + Months [date. getmonth ()] + "," + (Y2K (date. getyear () + "." //-Script> 10. How to display the countdown to a specific date <Script language = "JavaScript"> < Function Y2K (number) {return (number <1000 )? Number + 1900: Number ;} Function timetilldate (whenday, whenmonth, whenyear ){ VaR now = new date (); VaR ThisDay = now. getdate (), thismonth = now. getmonth () + 1, Thisyear = Y2K (now. getyear ()) VaR yearsdifference = whenyear-Thisyear, monthsdifference = 0, daysdifference = 0, string = ''; If (whenmonth> = thismonth) monthsdifference = whenmonth-thismonth; Else {yearsdifference --; monthsdifference = whenmonth + 12-thismonth ;} If (whenday> = ThisDay) daysdifference = whenday-ThisDay; Else { If (monthsdifference> 0) monthsdifference --; Else {yearsdifference --; monthsdifference + = 11 ;} Daysdifference = whenday + 31-ThisDay; } If (yearsdifference <0) return ''; If (yearsdifference = 0) & (monthsdifference = 0) & (daysdifference = 0 )) Return ''; If (yearsdifference> 0) { string = yearsdifference + 'Year'; If (yearsdifference> 1) string + ='s '; string + = ''; } If (monthsdifference> 0) { string + = monthsdifference + 'month'; If (monthsdifference> 1) string + ='s '; string + = ''; }< br> If (daysdifference> 0) { string + = daysdifference + 'day '; If (daysdifference> 1) string + = 's'; string + = ''; } VaR Difference = date. UTC (now. getyear (), now. getmonth (), now. getdate (), now. gethours (), now. getminutes (), now. getseconds ())- Date. UTC (now. getyear (), now. getmonth (), now. getdate (), 0, 0 ); Difference = 1000*60*60*24-difference; VaR hoursdifference = math. Floor (difference/1000/60/60 ); Difference = difference-hoursdifference x 1000*60*60 VaR minutesdifference = math. Floor (difference/1000/60 ); Difference = difference-minutesdifference * 1000*60 VaR secondsdifference = math. Floor (difference/1000 ); If (hoursdifference> 0 ){ String + = hoursdifference + 'hour '; If (hoursdifference> 1) string + ='s '; String + = ''; } If (minutesdifference> 0 ){ String + = minutesdifference + 'minute '; If (minutesdifference> 1) string + ='s '; String + = ''; } If (secondsdifference> 0 ){ String + = secondsdifference + 'second '; If (secondsdifference> 1) string + ='s '; String + = ''; } Return string; } Call example, for example, how long is there till now at 31/12/1999. Document. Write (timetilldate (1999 )); //-Script> 11. How to remove hours from a date <Script language = 'javascript '> < VaR date = new date (); VaR date = new date (date. UTC (date. getyear (), date. getmonth (), date. getdate (), date. gethours (), date. getminutes (), date. getseconds ()-5x60x60*1000 ); Document. Write (date ); //-Script> 12. How to add a few months in a date and display it correctly <Script language = "JavaScript"> < Function makearray (){ For (I = 0; I <makearray. Arguments. length; I ++) This [I + 1] = makearray. Arguments [I]; } VaR months = new makearray ('january ', 'february', 'march', 'mongol ', 'May', 'june', 'july', 'August ', 'September ', 'October ', 'november', 'december '); Function nths (day ){ If (Day = 1 | day = 21 | day = 31) return 'st '; Else if (Day = 2 | day = 22) return 'nd '; Else if (Day = 3 | day = 23) return 'rd '; Else return 'th '; } Function Y2K (number) {return (number <1000 )? Number + 1900: Number ;} Function monthsahead (noofmonths ){ VaR today = new date (); VaR date = new date (today. getyear (), today. getmonth () + noofmonths, today. getdate (), today. gethours (), today. getminutes (), today. getseconds ()) Return date. getdate () + nths (date. getdate () + ''+ months [date. getmonth () + 1] + ''+ Y2K (date. getyear ()) } call example: document. Write (monthsahead (6); //-script> |