{"id":1581,"date":"2011-09-20T11:34:37","date_gmt":"2011-09-20T16:34:37","guid":{"rendered":"http:\/\/icesquare.com\/wordpress\/?p=1581"},"modified":"2012-08-14T18:07:46","modified_gmt":"2012-08-14T23:07:46","slug":"weird-javascript-date-bug","status":"publish","type":"post","link":"https:\/\/icesquare.com\/wordpress\/weird-javascript-date-bug\/","title":{"rendered":"Weird JavaScript Date Bug"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wordpress\/wp-content\/uploads\/2011\/09\/bug.png\" alt=\"\" title=\"Bug\" class=\"alignleft\" \/><\/p>\n<p style=\"margin-top:30px;\">Today I found a very weird bug in JavaScript &#8211; The date function. I never expected to see a bug in the standard JavaScript implementation. Here is my code. It does nothing more than adding 21 days on the reference date:<\/p>\n<pre style=\"margin-top:60px;\">\r\nvar referenceDateObject\t= new Date(2011, 9, 10);\r\nvar deliveryDateObj \t= new Date(referenceDateObject.getTime() + 24 * 60 * 60 * 1000 * 21);\r\nvar deliveryDateMonth\t= parseInt(deliveryDateObj.getUTCMonth());\r\nvar deliveryDateString\t= deliveryDateObj.getUTCFullYear() + '-' + deliveryDateMonth + '-' +  deliveryDateObj.getUTCDate();\r\nalert(deliveryDateString);\r\n<\/pre>\n<p>Given a reference date of September 10, 2011, I expect the date should be October 1, 2011. However, it ends up returning me September 31, which doesn&#8217;t exist!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"231\" height=\"135\" data-attachment-id=\"1588\" data-permalink=\"https:\/\/icesquare.com\/wordpress\/weird-javascript-date-bug\/september-31\/#main\" data-orig-file=\"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/09\/September-31.png\" data-orig-size=\"231,135\" data-comments-opened=\"1\" data-image-title=\"September 31\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/09\/September-31.png\" src=\"\/wordpress\/wp-content\/uploads\/2011\/09\/September-31.png\" alt=\"\" title=\"September 31\" class=\"alignleft size-full wp-image-1588\" \/><\/p>\n<p>I test that on Firefox and Google Chrome, both give the same problem. A quick fix will be moving the date calculation to the server side.<\/p>\n<p>P.S. MySQL date function automatically convert 9\/31 to 10\/1, so the data on the server looks fine.<\/p>\n<p>Update &#8211; That&#8217;s my fault. The Date object treats the month from 0-11 instead of 1-12. Here is a corrected version:<\/p>\n<pre style=\"margin-top:60px;\">\r\nvar referenceDateObject\t= new Date(2011, 8, 10);   \/\/That's September 10, 2011, not August 10.\r\nvar deliveryDateObj \t= new Date(referenceDateObject.getTime() + 24 * 60 * 60 * 1000 * 21);\r\nvar deliveryDateMonth\t= parseInt(deliveryDateObj.getUTCMonth()) + 1;\r\nvar deliveryDateString\t= deliveryDateObj.getUTCFullYear() + '-' + deliveryDateMonth + '-' +  deliveryDateObj.getUTCDate();\r\nalert(deliveryDateString);\r\n<\/pre>\n<p>That will do the trick.<\/p>\n<p>&#8211;Derrick<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I found a very weird bug in JavaScript &#8211; The date function. I never expected to see a bug in the standard JavaScript implementation. Here is my code. It <a class=\"more-link\" href=\"https:\/\/icesquare.com\/wordpress\/weird-javascript-date-bug\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[94,141,52,6],"tags":[15,125],"class_list":["post-1581","post","type-post","status-publish","format-standard","hentry","category-computer-problems-computer-help-pc-support","category-programming-problems","category-software-engineering","category-technology","tag-bug","tag-javascript"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p1sqzr-pv","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":328,"url":"https:\/\/icesquare.com\/wordpress\/ie-is-fxxking-but-it-is-not-an-excuse\/","url_meta":{"origin":1581,"position":0},"title":"IE is &#8220;fxxking&#8221;, but it is not an excuse to leave bug in the codes","author":"Derrick","date":"October 29, 2009","format":false,"excerpt":"Today, my co-worker found that the web-application he developed worked only in Firefox, and it didn't work in Internet Explorer(IE). He immediately asked me if we should ignore the IE users or not. I know that he was working on JQuery(Javascript). It is not surprised that IE does not support\u2026","rel":"","context":"In &quot;Programming Problems&quot;","block_context":{"text":"Programming Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/programming-problems\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":621,"url":"https:\/\/icesquare.com\/wordpress\/thought-on-opera-mini-for-iphone\/","url_meta":{"origin":1581,"position":1},"title":"Thought on Opera Mini for iPhone","author":"Derrick","date":"April 13, 2010","format":false,"excerpt":"After trying the Opera Mini for iPhone, here is my comment: Zoom is missing. You can't use finger to zoom in\/out the page. It makes it very hard to browse the full-size page from the phone. Poor support on JavaScript. Website that works fine on Safari doesn't mean they will\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2428,"url":"https:\/\/icesquare.com\/wordpress\/vlc-crashes-playing-real-media-movie\/","url_meta":{"origin":1581,"position":2},"title":"[Solved]VLC Crashes when playing Real Media Movie","author":"Derrick","date":"December 28, 2011","format":false,"excerpt":"Every time when I open a real media movie using VLC (VideoLAN) player, and when I try to fast forward or drag the time to a certain position, it crashes. It is very annoying because VLC is one of very few movie players that support real media format, especially on\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":832,"url":"https:\/\/icesquare.com\/wordpress\/think-twice-before-upgrading-your-server-to-php-5-3-3\/","url_meta":{"origin":1581,"position":3},"title":"Think twice before upgrading your server to PHP 5.3.3","author":"Derrick","date":"September 6, 2010","format":false,"excerpt":"I upgraded to PHP 5.3.3 today, and I was shocked! I found a huge security hole default setting change that may leak the source code to public, which never happened in the earlier version. Here is an example. Suppose my code looks like this: <?php echo \"This is a good\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"Good Example","src":"\/wordpress\/wp-content\/uploads\/2010\/09\/php_5-3-3_goodExample-300x163.png","width":350,"height":200},"classes":[]},{"id":62,"url":"https:\/\/icesquare.com\/wordpress\/shit-is-priced-differently-in-different-region\/","url_meta":{"origin":1581,"position":4},"title":"Shit is priced differently in different region.","author":"Derrick","date":"May 21, 2009","format":false,"excerpt":"I was experimenting the salary search engine at Indeed.com today, and I found some interesting result... \"The salaries of Shit in different locations in US\" Here is the result: Or you can try to click here to repeat the test.... What makes me couldn't stop laughing is the statistical conclusion\u2026","rel":"","context":"In &quot;Fun Stuffs&quot;","block_context":{"text":"Fun Stuffs","link":"https:\/\/icesquare.com\/wordpress\/category\/fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2036,"url":"https:\/\/icesquare.com\/wordpress\/solvedtftp-error-code-2-access-violation\/","url_meta":{"origin":1581,"position":5},"title":"[Solved]TFTP: Error code 2: Access violation","author":"Derrick","date":"December 13, 2011","format":false,"excerpt":"This post is an extension of my original post on TFTP: How to Setup TFTP. Although the post was written for Ubuntu, the same idea can be applied to all systems. So, here is our problem. You set up a TFTP server. It starts fine. You upload a file, it\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/11\/TFTP_Small.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1581"}],"version-history":[{"count":10,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1581\/revisions"}],"predecessor-version":[{"id":2867,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1581\/revisions\/2867"}],"wp:attachment":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}