{"id":1587,"date":"2024-09-19T18:20:36","date_gmt":"2024-09-19T16:20:36","guid":{"rendered":"https:\/\/cln.io\/blog\/?p=1587"},"modified":"2024-09-21T17:32:35","modified_gmt":"2024-09-21T15:32:35","slug":"tines-lambda-for-generating-month-start-end-dates-for-the-last-12-months-or-whatever-period-youd-like","status":"publish","type":"post","link":"https:\/\/cln.io\/blog\/tines-lambda-for-generating-month-start-end-dates-for-the-last-12-months-or-whatever-period-youd-like\/","title":{"rendered":"Tines: Lambda for Generating Month Start\/End Dates for the last 12 months (or whatever period you&#8217;d like)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">NOTE: I wrote this blogpost with Claude Sonet 3.5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I want to share a lambda function I&#8217;ve built that might prove incredibly useful for your future projects. This function generates the exact start and end moments for the last 12 months, providing a comprehensive date range that can be invaluable for various data analysis and reporting tasks.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"yaml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MAP_LAMBDA(\n  RANGE(1, 12),\n  LAMBDA(months, {\n    \"months\":months,\n    \"target_month\": DATE_PARSE(DATE(months &amp; \" month ago\")).month,\n    \"target_year\": DATE_PARSE(DATE(months &amp; \" month ago\")).year,\n    \"start_of_month\": DATE(\"midnight on \" &amp; DATE(months &amp;\" months ago\", '%B %Y')),\n    \"end_of_month\": DATE(DATE(months -1 &amp;\" months ago\", '%B %Y'), '%s') -1|> DATE(%)\n  })\n)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What Does It Do?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This lambda function performs the following tasks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It iterates through the last 12 months (using <code>RANGE(1, 12)<\/code>).<\/li>\n\n\n\n<li>For each month, it calculates:\n<ul class=\"wp-block-list\">\n<li>The number of months ago<\/li>\n\n\n\n<li>The target month and year<\/li>\n\n\n\n<li>The exact start of the month (midnight on the first day)<\/li>\n\n\n\n<li>The exact end of the month (last second of the last day)<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">The Output<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The function returns an array of objects, each representing a month. Here&#8217;s a snippet of the output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[\n  {\n    \"months\": 1,\n    \"target_month\": 8,\n    \"target_year\": 2024,\n    \"start_of_month\": \"2024-08-01T00:00:00.000+00:00\",\n    \"end_of_month\": \"2024-08-31T23:59:59.000+00:00\"\n  },\n  \/\/ ... (10 more entries)\n  {\n    \"months\": 12,\n    \"target_month\": 9,\n    \"target_year\": 2023,\n    \"start_of_month\": \"2023-09-01T00:00:00.000+00:00\",\n    \"end_of_month\": \"2023-09-30T23:59:59.000+00:00\"\n  }\n]<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-1024x683.jpg\" alt=\"\" class=\"wp-image-1622\" srcset=\"https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-1024x683.jpg 1024w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-300x200.jpg 300w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-768x512.jpg 768w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-1536x1025.jpg 1536w, https:\/\/cln.io\/blog\/wp-content\/uploads\/2024\/09\/towfiqu-barbhuiya-bwOAixLG0uc-unsplash-2048x1366.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@towfiqu999999?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Towfiqu barbhuiya<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/a-calendar-with-red-push-buttons-pinned-to-it-bwOAixLG0uc?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOTE: I wrote this blogpost with Claude Sonet 3.5 I want to share a lambda function I&#8217;ve built that might prove incredibly useful for your future projects. This function generates the exact start and end [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,59],"tags":[],"class_list":["post-1587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation","category-tines"],"_links":{"self":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/comments?post=1587"}],"version-history":[{"count":3,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":1623,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/posts\/1587\/revisions\/1623"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/media\/1624"}],"wp:attachment":[{"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/media?parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/categories?post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cln.io\/blog\/wp-json\/wp\/v2\/tags?post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}