Addition and subtraction of dates in JS

Source: Internet
Author: User
Tags abs dateformat i18n

JavaScript implementation date subtraction Calculation function code instance, because there is no similar adddays method in C # in JS, so if you want to implement date addition and subtraction, you need to write the function to achieve.

Today even if the date in the JS add and subtract to make a summary, a total of two steps

The first step, the introduction of date.format.js, the role of this JS is to convert the date to the specified format, the code is as follows

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 var dateformat = function () {var token =/d{1,4}|m{1,4}|yy (?: yy)? | ([HHMSTT]) 1?| [llosz]|] [^"]*"|' [^ ']* '/g, timezone =/b (?: [Pmcea][sdp]t| (?:P acific| mountain| central| eastern| Atlantic) (?: standard| Daylight| prevailing) time| (?: gmt| UTC) (?: [-+]d{4})? b/g, timezoneclip =/[^-+da-z]/g, pad = function (val, len) {val = String (val); len = Len | | 2; while (Val.length < le N) val = "0" + val; return Val; };  //regexes and supporting functions are cached through return function (date, closure, UTC) {var DF = mask Mat  //Can ' t provide UTC if you skip other args (Use the "UTC: Mask prefix) if (arguments.length = 1 && O Bject.prototype.toString.call (date) = = "[Object String]" &&!/d/.test (date) {mask = date; date = undefined;} &n Bsp Passing date through date applies date.parse, if necessary date = date? New Date: new Date; if (isNaN (date)) throw SyntaxError ("Invalid Date");   Mask = String (Df.masks[mask] | | | mask | | df.masks["Default"]);  //Allow setting the UTC argument via the Mask if (Mask.slice (0, 4) = = "UTC:") {mask = Mask.slice (4); UTC = True; }   var _ = UTC? "GETUTC": "get", D = date[_ + "date"] (), d = date[_ + "Day" (), M = date[_ + "Month"] (), y = date[_ + "Fullyear"] (), H = Date[_ + "Hours"] (), M = date[_ + "Minutes"] (), s = date[_ + "Seconds"] (), L = Date[_ + "milliseconds"] (), o = UTC? 0:date.gettimezoneoffset (), flags = {D:D, dd:pad (d), ddd:df.i18n.daynames[d], Dddd:df.i18n.daynames[d + 7], m:m + 1, Mm:pad (M + 1), mmm:df.i18n.monthnames[m], mmmm:df.i18n.monthnames[m +), yy:string (y). Slice (2), yyyy:y, h:h% 1 2 | | Hh:pad (h%), H:h, Hh:pad (h), M:m, Mm:pad (M), S:s, Ss:pad (s), L:pad (L, 3), L:pad (L > 99? Math.Round (L/10): L), T:h < 12? "A": "P", tt:h < 12? "AM": "PM", t:h < 12? "A": "P", Tt:h < 12? "AM": "PM", Z:UTC? "UTC": (String (date). Match (timezone) | | [""]). POPs (). replace (Timezoneclip, ""), O: (o > 0?) -" : "+") + pad (math.abs (o)/(Math.floor) + math.abs (o)%, 4), S: ["th", "st", "nd", "rd"][d% > 3? 0: (d% 10 0-d%!=) * d% 10]};   Return Mask.replace (token, function ($) {return $ in flags flags[$0]: $0.slice (1, $0.length-1);}); }; }();  //Some common Format Strings dateformat.masks = {"Default": "DDD mmm dd yyyy HH:MM:ss", ShortDate: "M/d/yy", Medi Umdate: "Mmm D, yyyy", longdate: "Mmmm d, yyyy", fulldate: "dddd, mmmm d, yyyy", Shorttime: "h:mm TT", Mediumtime: "H:mm:s S TT ", longtime:" H:mm:ss tt Z ", Isodate:" Yyyy-mm-dd ", Isotime:" HH:MM:ss ", Isodatetime:" Yyyy-mm-dd ' T ' HH:MM:ss ", ISOUTCD Atetime: "Utc:yyyy-mm-dd ' T ' HH:MM:ss ' Z '"};  //Internationalization strings dateformat.i18n = {daynames: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "S Unday "," Monday "," Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "], monthnames: [" A Few "," Feb "," Mar "," APR "," "M Ay "," June "," Aug "," Nov "," Sep "," Oct "," January "," Dec "," February "," March "," April "," may "," June "," July "," August "," September "," October "," November "," December "]};  /For convenience ... Date.prototype.format = function (mask, UTC) {return DateFormat (this, mask, UTC);

In the second step, the time is converted to milliseconds to add and subtract calculations, and then use the above JS to the specified date

?

1 2 3 4 5 var nowdate = new Date (); Addition operation var ysdate = new Date (Nowdate.gettime () -30*24*60*60*1000). Format (' Yyyy-mm-dd '); Subtraction operation var yedate = new Date (Nowdate.gettime () -24*60*60*1000). Format (' yyyy-m-d ');

Through the above two steps to achieve, JS in the date of addition and subtraction, how simple it! Go ahead and try!

The above mentioned is the entire content of this article, I hope you can enjoy.

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.