{"id":231,"date":"2012-03-12T14:52:35","date_gmt":"2012-03-12T14:52:35","guid":{"rendered":"https:\/\/pim.famnit.upr.si\/wp\/?p=231"},"modified":"2021-11-17T11:20:36","modified_gmt":"2021-11-17T11:20:36","slug":"javascript-mootools-random-date-generator","status":"publish","type":"post","link":"https:\/\/pim.famnit.upr.si\/wp\/?p=231","title":{"rendered":"Javascript (Mootools) random date generator"},"content":{"rendered":"<p>I&#8217;ve needed a random date generator between two dates. This has been easy with Mootools. Both dates are first converted to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Unix_time\">unix timestamp<\/a> format (# of seconds after 1.1.1970), then a random number between the two unix times is generated and converted back to date.<\/p>\n<p><\/p>\n<p><\/p>\n<pre style=\"background-color: #ffffc3;\">function randomDate(date1, date2) {\r\n&nbsp;&nbsp; var minD = new Date().parse(date1).format('%s');\r\n&nbsp;&nbsp; var maxD = new Date().parse(date2).format('%s');\r\n&nbsp;&nbsp; var random = Number.random(parseInt(minD), parseInt(maxD));\r\n&nbsp;&nbsp; var randomDate = new Date().parse(random+\"000\").format('db'); \r\n}<\/pre>\n<p>Which can be called <\/p>\n<pre style=\"background-color: #ffffc3;\">var randomDateTmp = randomDate('1999-06-08 16:34:52', new Date()); \r\n<\/pre>\n<p>Set the format accordingly to <a href=\"http:\/\/mootools.net\/docs\/more\/Types\/Date#Date:format\">http:\/\/mootools.net\/docs\/more\/Types\/Date#Date:format<\/a><\/p>\n<p>This can be easily accomplished in other programming languages. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve needed a random date generator between two dates. This has been easy with Mootools. Both dates are first converted to unix timestamp format (# of seconds after 1.1.1970), then a random number between the two unix times is generated and converted back to date. function randomDate(date1, date2) { &nbsp;&nbsp; var minD = new Date().parse(date1).format(&#8216;%s&#8217;);&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=231"}],"version-history":[{"count":1,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":750,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=\/wp\/v2\/posts\/231\/revisions\/750"}],"wp:attachment":[{"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pim.famnit.upr.si\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}