Javascript: Converting A String, Ie. "1 Hour 2 Minutes" To Time In Seconds
Solution 1:
I would write a function that repeatedly matches "(digit+) (timeUnit)
" and does the arithmetic in milliseconds because they are more common units. Something like this:
var timespanMillis = (function() {
var tMillis = {
second: 1000,
min: 60 * 1000,
minute: 60 * 1000,
hour: 60 * 60 * 1000// etc.
};
returnfunction(s) {
var regex = /(\d+)\s*(second|min|minute|hour)/g, ms=0, m, x;
while (m = regex(s)) {
x = Number(m[1]) * (tMillis[m[2]]||0);
ms += x;
}
return x ? ms : NaN;
};
})();
timespanMillis("2 mins"); // => 120000timespanMillis("10 seconds"); // => 10000timespanMillis("1 hour and 4 minutes"); // => 3840000timespanMillis("Foobar"); // => NaN
The trick is to keep the tMillis lookup object in sync with the regex but it shouldn't be too hard; for example, you could construct the regular expression in the closure by joining the properties of tMillis as the source string.
Solution 2:
Once you have decided on the format, you need to write a script, that converts your format of time to standard format or just extracts the essential numbers from your format.
After that, you can use the date constructor to parse a date, then write get the seconds from the getTime() method
var d=newDate("October 12, 1987 10:23:00");
document.write(d.getTime() + " milliseconds since 1970/01/01");
Solution 3:
I wrote an Open source library MgntUtils in java (not javascript) that answers in part to this requirement. It contains a static method parsingStringToTimeInterval(String value)
this method parses a string that is expected to hold some time interval value - a numeric value with optional time unit suffix. For example, string "38s" will be parsed as 38 seconds, "24m" - 24 minutes "4h" - 4 hours, "3d" - 3 days and "45" as 45 milliseconds. Supported suffixes are "s" for seconds, "m" for minutes, "h" for hours, and "d" for days. String without suffix is considered to hold a value in milliseconds. Suffixes are case insensitive. If provided String contains an unsupported suffix or holds negative numeric value or zero or holds a non-numeric value - then IllegalArgumentException is thrown. This method returns TimeInterval class - a class also defined in this library. Essentially, it holds two properties with relevant getters and setters: long "value" and java.util.concurrent.TimeUnit. But in addition to getters and setters this class has methods toMillis(), toSeconds(), toMinutes(), toHours() toDays(). Those methods return long vlaue in specified time scale (The same way as corresponding methods in class java.util.concurrent.TimeUnit)
This method may be very useful for parsing time interval properties such as timeouts or waiting periods from configuration files. It eliminates unneeded calculations from different time scales to milliseconds back and forth. Consider that you have a methodInvokingInterval property that you need to set for 5 days. So in order to set the milliseconds value you will need to calculate that 5 days is 432000000 milliseconds (obviously not an impossible task but annoying and error prone) and then anyone else who sees the value 432000000 will have to calculate it back to 5 days which is frustrating. But using this method you will have a property value set to "5d" and invoking the code
long seconds = TextUtils.parsingStringToTimeInterval("5d").toSeconds();
will solve your conversion problem. Obviously, this is not overly complex feature, but it could add simplicity and clarity in your configuration files and save some frustration and "stupid" miscalculation into milliseconds bugs. Here is the link to the article that describes the MgntUtils library as well as where to get it: MgntUtils
Post a Comment for "Javascript: Converting A String, Ie. "1 Hour 2 Minutes" To Time In Seconds"