{"id":714,"date":"2010-05-14T14:20:08","date_gmt":"2010-05-14T08:50:08","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=714"},"modified":"2016-12-19T15:04:55","modified_gmt":"2016-12-19T09:34:55","slug":"grails-date-difference-with-finer-details","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-date-difference-with-finer-details\/","title":{"rendered":"Grails : Difference b\/w two dates with fine-grained details"},"content":{"rendered":"<p>Hi Friends,<\/p>\n<p>To retrieve difference between two dates with finer details which would display their difference as 2 years 7 weeks 3 days 2 hours 5 mins ago is given below.<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n \/**\r\n   * Returns differnce b\/w new Date and old date as Map holding difference in years, weeks, days, hrs, mins & secs \r\n   *\/\r\n  public static Map getDiffernceInDates(Date oldDate, Date newDate = new Date()) {\r\n    Long difference = newDate.time - oldDate.time\r\n    Map diffMap =[:]\r\n    difference = difference \/ 1000\r\n    diffMap.seconds = difference % 60\r\n    difference = (difference - diffMap.seconds) \/ 60\r\n    diffMap.minutes = difference % 60\r\n    difference = (difference - diffMap.minutes) \/ 60\r\n    diffMap.hours = difference % 24\r\n    difference = (difference - diffMap.hours) \/ 24\r\n    diffMap.years = (difference \/ 365).toInteger()\r\n    if(diffMap.years)\r\n       difference = (difference) % 365\r\n    diffMap.days = difference % 7\r\n    diffMap.weeks = (difference - diffMap.days) \/ 7  \r\n    return diffMap\r\n  }\r\n  \r\n<\/pre>\n<\/blockquote>\n<p>The taglib to display the date difference in detail is given below <\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\ndef timeDiffInDetail = {attrs ->\r\n        Map diff = DateUtil.getDiffernceInDates(attrs.oldDate, attrs.newDate ?: new Date())\r\n        String result =  diff.years ? diff.years + \" years \" : \"\"\r\n        result += diff.weeks ? diff.weeks + \" weeks \" : \"\"\r\n        result += diff.days ? diff.days + \" days \" : \"\"\r\n        result += diff.hours ? diff.hours + \" hours \" : \"\"\r\n        result += diff.minutes ? diff.minutes + \" minutes ago\" : \"\"\r\n        if (result)\r\n          out << result\r\n        else\r\n          out << \" 0 minutes ago\"\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>Hope this helpled!<\/p>\n<p>Cheers!<\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Friends, To retrieve difference between two dates with finer details which would display their difference as 2 years 7 weeks 3 days 2 hours 5 mins ago is given below. \/** * Returns differnce b\/w new Date and old date as Map holding difference in years, weeks, days, hrs, mins &#038; secs *\/ public [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[7],"tags":[241,4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/714"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=714"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/714\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}