Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Last active December 16, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biovisualize/5430237 to your computer and use it in GitHub Desktop.
Save biovisualize/5430237 to your computer and use it in GitHub Desktop.
Parse dates with quarters (i.e., "Q4-2010")
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script>
var dates = ["Q4-2010", "Q1-2011", "Q2-2011", "Q3-2011", "Q4-2011", "Q1-2012", "Q2-2012", "Q3-2012","Q4-2012"];
var dateParsed = dates.map(function(d, i){
var splitted = d.split('-');
var quarterEndMonth = splitted[0].charAt(1) * 3;
var quarterStartMonth = quarterEndMonth - 3;
return [d3.time.format('%m %Y').parse(quarterStartMonth + ' ' + splitted[1]),
d3.time.format('%m %Y').parse(quarterEndMonth + ' ' + splitted[1])];
});
console.log(JSON.stringify(dateParsed));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment