PDA

View Full Version : Time Calculation


David Miller
October 6th, 2010, 09:28 PM
Wondering if the is a way to calculate a new time that is 30 minutes before the time provided in the data?

Time = 6:00 PM
Need to return = 5:30 PM

Haven't been able to find this one here. Thanks.

-Dave Miller

Dan Korn
October 7th, 2010, 03:01 PM
This is a great question. Actually, once you have the time in a JavaScript Date object (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date), doing the adjustment is really easy, and returning it with the FormatDate function (http://forums.printable.com/showthread.php?p=195#post195) is pretty easy too.

The hard part is parsing the time in the first place. There's really nothing built into either JavaScript or FusionPro for this. However, the great thing about JavaScript is that just about every problem has already been solved, so a quick Internet search for "JavaScript parse time" turns up several hits, including this one (http://stackoverflow.com/questions/141348/what-is-the-best-way-to-parse-a-time-into-a-date-object-from-user-input-in-javasc/2307344#2307344), which I've borrowed here:
String.prototype.reverse = function() {
var splitext = this.split("");
var revertext = splitext.reverse();
var reversed = revertext.join("");
return reversed;
}

String.prototype.trim = function () {
return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/**
* Parse a string that looks like time and return a date object.
* @return Date object on success, false on error.
*/
String.prototype.parseTime = function() {
// trim it and reverse it so that the minutes will always be greedy first:
var value = this.trim().reverse();

// We need to reverse the string to match the minutes in greedy first, then hours
var timeParts = value.match(/(a|p)?\s*((\d{2})?:?)(\d{1,2})/i);

// This didnt match something we know
if (!timeParts) {
return false;
}

// reverse it:
timeParts = timeParts.reverse();

// Reverse the internal parts:
for( var i = 0; i < timeParts.length; i++ ) {
timeParts[i] = timeParts[i] === undefined ? '' : timeParts[i].reverse();
}

// Parse out the sections:
var minutes = parseInt(timeParts[1], 10) || 0;
var hours = parseInt(timeParts[0], 10);
var afternoon = timeParts[3].toLowerCase() == 'p' ? true : false;

// If meridian not set, and hours is 12, then assume afternoon.
afternoon = !timeParts[3] && hours == 12 ? true : afternoon;
// Anytime the hours are greater than 12, they mean afternoon
afternoon = hours > 12 ? true : afternoon;
// Make hours be between 0 and 12:
hours -= hours > 12 ? 12 : 0;
// Add 12 if its PM but not noon
hours += afternoon && hours != 12 ? 12 : 0;
// Remove 12 for midnight:
hours -= !afternoon && hours == 12 ? 12 : 0;

// Check number sanity:
if( minutes >= 60 || hours >= 24 ) {
return false;
}

// Return a date object with these values set.
var d = new Date();
d.setHours(hours);
d.setMinutes(minutes);
return d;
}

var time = Field("time").parseTime();
time.setMinutes(time.getMinutes() - 30);
return FormatDate(time, "h:nn A");
You should be able to simply change the data field name in the third-to-last line above to match your data.

David Miller
October 12th, 2010, 07:07 PM
This works perfectly. Many Thanks! -Dave