I need to select appropriate date/time format depending on the date range I am working with. The date range could be a few weeks, days, hours, seconds (even milliseconds).
I've looked into moment.js and format.js but I'm hoping there's something better out there. I want to achieve something like highcharts daterange. These charts select appropriate formats for the datetime axis according to the range.
Any help would be appreciated.
Try this below code, it is still rough:
var start = new Date(2014,12,22);
var end = new Date(2014,12,23);
var range = end.getTime() - start.getTime(); // milliseconds
var seconds = range/1000;
var minutes = seconds/60;
var hour = minutes/60;
var days = hour/24;
var months = days/30;
var year = months/12;
var maxValue = [9999, 12, 31, 24, 60, 60];
var dateRange = [year, months, days, hour, minutes, seconds];
var dateFormat = ['#yr', '#mt', '#d', '#h', '#min', '#sec'];
var specifiedIndex = 0; // default format
for (var index = 0; index < maxValue.length; index ++)
{
if ((dateRange[index] < maxValue[index]) && (dateRange[index] >= 1))
{
specifiedIndex = index;
index = maxValue.length;
}
}
alert("format date = " + dateFormat[specifiedIndex]);
Thank you,