From 1e61298d7c1c7bba5f4777a27e6ee29f498544df Mon Sep 17 00:00:00 2001 From: Olly Smith Date: Thu, 23 Feb 2012 22:15:07 +0000 Subject: [PATCH] YYYY-MM-DD date support (closes #2) --- morris.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/morris.js b/morris.js index 3e5f81e..7fe7121 100644 --- a/morris.js +++ b/morris.js @@ -1,17 +1,30 @@ /*global jQuery: false, Raphael: false */ -function parse_year(year) { - var m = year.toString().match(/(\d+) Q(\d)/); - var n = year.toString().match(/(\d+)\-(\d+)/); +function parse_year(date) { + var m = date.toString().match(/^(\d+) Q(\d)$/); + var n = date.toString().match(/^(\d+)-(\d+)$/); + var o = date.toString().match(/^(\d+)-(\d+)-(\d+)$/) if (m) { return parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12; } else if (n) { return parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12; } + else if (o) { + // parse to a timestamp + var year = parseInt(o[1], 10); + var month = parseInt(o[2], 10); + var day = parseInt(o[3], 10); + var timestamp = new Date(year, month - 1, day).getTime(); + // get timestamps for the beginning and end of the year + var y1 = new Date(year, 0, 1).getTime(); + var y2 = new Date(year+1, 0, 1).getTime(); + // calculate a decimal-year value + return year + (timestamp - y1) / (y2 - y1); + } else { - return parseInt(year, 10); + return parseInt(date, 10); } }