{"id":6332,"date":"2022-06-21T15:50:23","date_gmt":"2022-06-21T14:50:23","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6332"},"modified":"2022-07-25T08:35:40","modified_gmt":"2022-07-25T07:35:40","slug":"javascript-anniversary-calculation","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-anniversary-calculation\/","title":{"rendered":"Javascript Anniversary Calculation"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">const getDate = (year, month, day) => new Date(year, month-1, day);\nconst isLeapYear = (year) => ((year % 4 === 0) &amp;&amp; (year % 100 !== 0)) || (year % 400 === 0); \nconst getDaysInMonth = (year, month) => [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];\nconst editDate = (d, fn) => { \n    const nd = new Date(d);\n    fn(nd);\n    return nd;\n};\nconst addYears = (date, years) => editDate(date, d => d.setFullYear(d.getFullYear() + years));\nconst addDays = (date, days) => editDate(date, d => d.setDate(d.getDate() + days));\nconst addMonths = function (date, value) {\n    const newDate = new Date(date);\n    var n = newDate.getDate();\n    newDate.setDate(1);\n    newDate.setMonth(newDate.getMonth() + value);\n    newDate.setDate(Math.min(n, getDaysInMonth(newDate.getFullYear(), newDate.getMonth())));\n    return newDate;\n};\nconst date = getDate(2000, 8, 31);\nconst anniversary12 = addYears(date, 12);\nlet anniversary125 = addMonths(anniversary12, 6);\nif (anniversary125.getDate() &lt; date.getDate()) {\n    anniversary125 = addDays(anniversary125, 1);\n}\nconsole.log(anniversary12, anniversary125)\n\/\/ 1 March\n<\/pre><\/div>\n\n\n\n<p>C# Version<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">var date = newDateTime(2000, 8, 31);\nvar anniversary = date.AddYears(12).AddMonths(6);\n\nif(anniversary.Day &lt; date.Day) {\n    anniversary = anniversary.AddDays(1);\n} \n\nConsole.WriteLine(anniversary);\n\/\/ 1 March<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>C# Version<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,5,4,1],"tags":[],"class_list":["post-6332","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=6332"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6332\/revisions"}],"predecessor-version":[{"id":6340,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6332\/revisions\/6340"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}