diff --git a/demos/form2/index.html b/demos/form2/index.html index ff308ee6..0fbe2b56 100644 --- a/demos/form2/index.html +++ b/demos/form2/index.html @@ -4,15 +4,8 @@ ox.js form demo - + - \ No newline at end of file diff --git a/source/Ox/js/Format.js b/source/Ox/js/Format.js index 325bb596..2ff2e914 100644 --- a/source/Ox/js/Format.js +++ b/source/Ox/js/Format.js @@ -358,6 +358,8 @@ Ox.formatDateRangeDuration Formats the duration of a date range as a string A date range is a pair of arbitrary-presicion date strings > Ox.formatDateRangeDuration('2000-01-01 00:00:00', '2001-03-04 04:05:06') '1 year 2 months 3 days 4 hours 5 minutes 6 seconds' + > Ox.formatDateRangeDuration('2000', '2001-01-01 00:00:01') + '1 year 1 second' > Ox.formatDateRangeDuration('1999', '2000', true) '1 year' > Ox.formatDateRangeDuration('2000', '2001', true) @@ -402,49 +404,73 @@ Ox.formatDateRangeDuration = function(start, end, utc) { /*@ Ox.formatDuration Formats a duration as a string - > Ox.formatDuration(123456.789, 3) - "1:10:17:36.789" - > Ox.formatDuration(12345.6789) - "03:25:46" - > Ox.formatDuration(12345.6789, true) - "0:03:25:46" - > Ox.formatDuration(3599.999, 3) - "00:59:59.999" > Ox.formatDuration(3599.999) - "01:00:00" + '01:00:00' + > Ox.formatDuration(3599.999, 2) + '01:00:00.00' + > Ox.formatDuration(3599.999, 3) + '00:59:59.999' + > Ox.formatDuration(3599.999, 'short') + '1h' + > Ox.formatDuration(3599.999, 3, 'short') + '59m 59.999s' + > Ox.formatDuration(3599.999, 'long') + '1 hour' + > Ox.formatDuration(3599.999, 3, 'long') + '59 minutes 59.999 seconds' + > Ox.formatDuration(86520, 2) + '1:00:02:00.00' + > Ox.formatDuration(86520, 'long') + '1 day 2 minutes' + > Ox.formatDuration(31543203, 2) + '1:000:02:00:03.00' + > Ox.formatDuration(31543203, 'long') + '1 year 2 hours 3 seconds' + > Ox.formatDuration(0, 2) + '00:00:00.00' + > Ox.formatDuration(0, 'long') + '' @*/ -Ox.formatDuration = function(sec, dec, format) { - var format = arguments.length == 3 ? format : (Ox.isString(dec) ? dec : "short"), - dec = (arguments.length == 3 || Ox.isNumber(dec)) ? dec : 0, - sec = dec ? sec : Math.round(sec), +Ox.formatDuration = function(/*sec, dec, format*/) { + var format = Ox.isString(arguments[arguments.length - 1]) + ? arguments[arguments.length - 1] : '', + dec = Ox.isNumber(arguments[1]) ? arguments[1] : 0, + sec = Ox.round(arguments[0], dec), val = [ Math.floor(sec / 31536000), Math.floor(sec % 31536000 / 86400), Math.floor(sec % 86400 / 3600), Math.floor(sec % 3600 / 60), - format == "short" ? Ox.formatNumber(sec % 60, dec) : sec % 60 + Ox.formatNumber(sec % 60, dec) ], - str = { - medium: ["y", "d", "h", "m", "s"], - long: ["year", "day", "hour", "minute", "second"] - }, - pad = [0, 3, 2, 2, dec ? dec + 3 : 2]; - while (!val[0] && val.length > (format == "short" ? 3 : 1)) { + str = !format ? [] + : format == 'short' ? ['y', 'd', 'h', 'm', 's'] + : ['year', 'day', 'hour', 'minute', 'second'], + pad = [ + val[0].toString().length, + val[0] ? 3 : 1, + 2, + 2, + dec ? dec + 3 : 2 + ]; + Ox.print('val', val) + while (!val[0] && val.length > (!format ? 3 : 1)) { val.shift(); - str.medium.shift(); - str.long.shift(); + str.shift(); pad.shift(); } - while (format != "short" && !val[val.length - 1] && val.length > 1) { - val.pop(); - str.medium.pop(); - str.long.pop(); - } return Ox.map(val, function(v, i) { - return format == "short" ? Ox.pad(v, pad[i]) : - v + (format == "long" ? " " : "") + str[format][i] + - (format == "long" && v != 1 ? "s" : ""); - }).join(format == "short" ? ":" : " "); + var ret; + if (!format) { + ret = Ox.pad(v, pad[i]); + } else if (Ox.isNumber(v) ? v : parseFloat(v)) { + ret = v + (format == 'long' ? ' ' : '') + str[i] + + (format == 'long' && v != 1 ? 's' : ''); + } else { + ret = null; + } + return ret; + }).join(!format ? ':' : ' '); }; /*@