{"id":70,"date":"2018-09-25T02:05:10","date_gmt":"2018-09-25T02:05:10","guid":{"rendered":"https:\/\/www.aladan.net\/MIMicry\/?p=70"},"modified":"2018-09-25T02:05:10","modified_gmt":"2018-09-25T02:05:10","slug":"mimwal-code-for-datetime-midnight-in-local-timezone","status":"publish","type":"post","link":"https:\/\/www.aladan.net\/MIMicry\/2018\/09\/25\/mimwal-code-for-datetime-midnight-in-local-timezone\/","title":{"rendered":"MIMWAL code for DateTime midnight in local timezone"},"content":{"rendered":"<p>I needed to calculate a DateTime value for midnight in the local time zone, for comparison against other date fields in my policy.\u00a0 It&#8217;s painfully difficult to do, but just barely possible.\u00a0 Here&#8217;s my solution:<\/p>\n<pre>DateTimeNow() -&gt; $now_utc\r\nMid(DateTimeFormat($now_utc, \"zz\"), 1, 2) -&gt; $timezone_offset\r\nConvertToNumber(DateTimeFormat($now_utc, \"HH\")) -&gt; $utc_hours\r\nDateTimeFromString(DateTimeFormat($now_utc, \"dd\/MM\/yyyy\"), \"en-AU\") -&gt; $midnight_utc\r\nSubtract(24, ConvertToNumber($timezone_offset)) -&gt; $rollover_hour\r\nIIF(LessThan($utc_hours, $rollover_hour), \"0.00:00:00.0\", \"1.00:00:00.0\") -&gt; $timezone_day_fix\r\nConcatenate(\"-0.\", $timezone_offset, \":00:00.0\") -&gt; $timezone_hour_fix\r\nDateTimeAdd(DateTimeAdd($midnight_utc, $timezone_hour_fix), $timezone_day_fix) -&gt; $midnight_local<\/pre>\n<p>Please note that this only works for positive integer timezones (such as Australia\/East) but at least it handles daylight savings correctly.\u00a0 For non-integer timezones, the $rollover_hour calculation would need to be extended, and for negative timezones something else would probably need to be done but I haven&#8217;t thought it through properly.<\/p>\n<p>To be honest it would be a lot easier and more efficient to just write a custom activity!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I needed to calculate a DateTime value for midnight in the local time zone, for comparison against other date fields in my policy.\u00a0 It&#8217;s painfully difficult to do, but just barely possible.\u00a0 Here&#8217;s my solution: DateTimeNow() -&gt; $now_utc Mid(DateTimeFormat($now_utc, &#8220;zz&#8221;), 1, 2) -&gt; $timezone_offset ConvertToNumber(DateTimeFormat($now_utc, &#8220;HH&#8221;)) -&gt; $utc_hours DateTimeFromString(DateTimeFormat($now_utc, &#8220;dd\/MM\/yyyy&#8221;), &#8220;en-AU&#8221;) -&gt; $midnight_utc Subtract(24, ConvertToNumber($timezone_offset))<span class=\"post-excerpt-end\">&hellip;<\/span><\/p>\n<p class=\"more-link\"><a href=\"https:\/\/www.aladan.net\/MIMicry\/2018\/09\/25\/mimwal-code-for-datetime-midnight-in-local-timezone\/\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8,14],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-howto","category-mimservice","category-mimwal"],"_links":{"self":[{"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":1,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/posts\/70\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aladan.net\/MIMicry\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}