jquery time arithmetic, formatted method expansion

Source: Internet
Author: User
Tags month name

/*Function: Date plus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.addDays = Date.prototype.addDays | |function(n) {This.setdate (This.getdate () +n);ReturnThis;}/*function: Date minus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.minusDays = Date.prototype.minusDays | |function(n) {This.setdate (This.getdate ()-n);ReturnThis;}/*Function: Date plus n days parameter: N is the number of days returned: N Days after date*/Date.prototype.getWeek = Date.prototype.getWeek | |function(date) {var n =Date.getday ();var rtnstr = "";Switch(n) {Case 0: {rtnstr = "Sunday";Break; }Case 1: {rtnstr = "Monday";Break; }Case 2: {rtnstr = "Tuesday";Break; }Case 3: {rtnstr = "Wednesday";Break; }Case 4: {rtnstr = "Thursday";Break; }Case 5: {rtnstr = "Friday";Break; }Case 6: {rtnstr = "Saturday";Break; }Default: {rtnstr = "";Break; } }ReturnRtnstr;}/*Function: Converts a string to a Date object parameter: A string returned as a yyyy-mm-dd or dd/mm/yyyy: Date Object Note: Direct instantiation of date objects, such as new date ("2011-04-06") is not supported under IE*/Date.prototype.convertDate =function(date) {var flag =True;var Datearray = Date.split ("-");if (datearray.length! = 3) {Datearray = Date.split ("/");if (datearray.length! = 3) {ReturnNull; } flag =False; }var newdate =NewDate ();If(flag) {//Month starting from 0 newdate.setfullyear (datearray[0], datearray[1]-1, datearray[2]); }Else{newdate.setfullyear (datearray[2], datearray[1]-1, datearray[0]); } newdate.sethours (0, 0, 0);ReturnNewdate;};/*Function: Calculates the difference parameter between two dates: date is a DateTime object flag:ms-milliseconds, S-second, M-Min, H-hour, D-Day, M-month, Y-year return: current date and date two milliseconds/second/minute/hour/day*/Date.prototype.dateDiff =function(date, flag) {var mscount = 1; Date =NewDate (date);Switch(flag) {Case "MS": Mscount = 1;Break;Case "S":This.setmilliseconds (0); Date.setmilliseconds (0); Mscount = 1000;Break;Case "M":This.setseconds (0, 0); Date.setseconds (0, 0); Mscount = 60 * 1000;Break;Case "H":This.setminutes (0, 0, 0); Date.setminutes (0, 0, 0); Mscount = 60 * 60 * 1000;Break;Case "D":This.sethours (0, 0, 0, 0); Date.sethours (0, 0, 0, 0); Mscount = 24 * 60 * 60 * 1000;Break; }var diff =This.gettime ()-Date.gettime ();return Math.floor (diff/Mscount);};/*Function: Format date parameter: formatstr-format string D: Displays the day as a number without leading zeros, such as 1DD: Displays the day as a number with leading zeros, such as 01ddd: Displays the day as an abbreviated form, such as SUNDDDD: Displays the day as the full name, such as Sundaym: Displays the month as a number without leading zeros, as shown in January as 1MM: Displays the month as a number with leading zeros, such as 01MMM: Displays the month as an abbreviated form, such as janmmmm: Displays the month as the full month name, such as januaryyy: Displays the year in two-digit format yyyy: Year in four-digit format h: Displays the hour as a number without leading zeros using a 12-hour system Note | | Usage hh: Displays the hour as a number h with leading zeros using a 12-hour system: Use a 24-hour system to display the hour as a number without leading zeros hh: Displays the hour as a number m with leading zeros using a 24-hour system: Displays the minute as a number with no leading zeros mm: Displays the minute as a number s with leading zeros: Displays the second as a number SS without leading zeros : Displays seconds as a leading zero number L: Displays milliseconds as a number without leading zeros ll: Displays milliseconds as a number with leading zeros TT: Display AM/PMTT: Show am/pm return: date after formatting*/Date.prototype.format =function(FORMATSTR) {var date =This;/*Function: Fill 0 character parameter: value-the string to fill, length-total length return: populated string*/var zeroize =function(value, Length) {if (!Length) {length = 2; } value =NewString (value);for (var i = 0, zeros = "; I < (length-value.length); i++) {zeros + = ' 0 '; }return zeros +Value };return Formatstr.replace (/"[^"]* "|" [^ ']* ' |\b (?:d {1,4}| M{1,4}|yy (?: yy)? | ([HHMSTT]) \1?| [LLZ]) \b/g,Function ($) {Switch ($) {Case ' d ':ReturnDate.getdate ();Case ' DD ':ReturnZeroize (Date.getdate ());Case ' DDD ':return [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][date.getday ()];Case ' dddd ':return [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][date.getday ()];Case ' M ':return Date.getmonth () + 1;Case ' MM ':Return Zeroize (Date.getmonth () + 1); Case ' MMM ': return [' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', '][date.getmonth ', ' Sep ', ' Oct ', ' Nov ', ' Dec ' ()]; Case ' MMMM ': return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' No ' Vember ', ' December '][date.getmonth ()]; Case ' yy ': return new String (Date.getfullyear ()). substr (2); Case ' yyyy ': return Date.getfullyear (); Case ' H ': return date.gethours ()% 12 | | 12; Case ' hh ': Return Zeroize (date.gethours ()% 12 | | 12); Case ' H ': return date.gethours (); Case ' HH ': Return Zeroize (Date.gethours ()); Case ' m ': return Date.getminutes (); Case ' mm ': Return Zeroize (Date.getminutes ()); Case ' s ': return Date.getseconds (); Case ' SS ': Return Zeroize (Date.getseconds ()); Case ' l ': return Date.getmilliseconds (); Case ' ll ': Return Zeroize (Date.getmilliseconds ()); Case ' TT ': Return date.gethours () < 12? ' Am ': ' PM '; Case ' TT ': Return date.gethours () < 12? ' AM ': ' PM '; } });}

jquery time arithmetic, formatted method expansion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.