{"id":3556,"date":"2016-01-29T01:31:35","date_gmt":"2016-01-29T07:31:35","guid":{"rendered":"http:\/\/bililite.com\/blog\/?p=3556"},"modified":"2016-04-05T14:04:56","modified_gmt":"2016-04-05T20:04:56","slug":"odd-bug-with-dates","status":"publish","type":"post","link":"https:\/\/bililite.com\/blog\/2016\/01\/29\/odd-bug-with-dates\/","title":{"rendered":"Odd bug with Date&#8217;s"},"content":{"rendered":"<p>It's been almost a year since I last posted. I'm still programming, but it's mostly visible on <a href=\"https:\/\/github.com\/dwachss\/\">github<\/a>, especially my trying to help with jquery\/globalize by <a href=\"https:\/\/github.com\/jquery\/globalize\/pull\/447\">implementing nongregorian calendars<\/a>.<\/p>\n<p>I finally solved a bug that was, um, bugging me. In order to test my <a href=\"https:\/\/en.wikipedia.org\/wiki\/Julian_day\">Julian Day<\/a> routines, I needed to create a javascript Date at midnight UTC rather than local time. I thought I was clever when I did:<\/p>\n<pre><code class=\"language-javascript\" >d = new Date();\r\nd.setUTCFullYear( year );\r\nd.setUTCMonth( month );\r\nd.setUTCDate( date );\r\nd.setUTCHours( 0 );\r\nd.setUTCMinutes( 0 );\r\nd.setUTCSeconds( 0 );<\/code><\/pre>\n<p>And everything worked fine, until last night when I would set <code class=\"language-javascript\" >month=1<\/code> (February) but the month would end up as <code class=\"language-javascript\" >2<\/code> (March). This had never happened before, and the code hadn't changed. <\/p>\n<p>I finally realized that the date was 2016-01-28 St. Louis time at 2300, or 2016-01-29 UTC. So setting the year to a non-leap year like 2015, then setting the month to <code class=\"language-javascript\" >1<\/code> with <code class=\"language-javascript\" >setUTCMonth()<\/code> meant I was trying to set it to 2015-02-29, which <code class=\"language-javascript\" >Date<\/code> helpfully corrected to 2015-03-01, then the date was set correctly. The only way that bug would turn up is if my code was run on the last day of the month and tested with a shorter month.<\/p>\n<p>Turns out there's a much better way to set UTC time:<\/p>\n<pre><code class=\"language-javascript\" >d = new Date( Date.UTC( year, month, date ) )<\/code><\/pre>\n<p>And now I am wiser.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It's been almost a year since I last posted. I'm still programming, but it's mostly visible on github, especially my trying to help with jquery\/globalize by implementing nongregorian calendars. I finally solved a bug that was, um, bugging me. In order to test my Julian Day routines, I needed to create a javascript Date at [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3556"}],"collection":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/comments?post=3556"}],"version-history":[{"count":3,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":3561,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/posts\/3556\/revisions\/3561"}],"wp:attachment":[{"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/media?parent=3556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/categories?post=3556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bililite.com\/blog\/wp-json\/wp\/v2\/tags?post=3556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}