Calculating Time Differences?

J

JDVarney

I have two date fields, one startTime, and the other endTime. The times
occur in the same day. I want to calculate the value of the differences as
hours, for example 3:30 PM - 2:00 PM = 1.5 hours. I can't find a way to
calculate this. I'm new to manipulation XML and JScript. Any help would be
appreciated.

Thanks,

John
 
J

Josh Bertsch [MSFT]

The Data Validation sample in the SDK has some examples of
calculations involving dates and times.

[from Steve VD]
Mathmatical operations involving two time values don't make much sense
IMHO. Usually it's a time +/- an offset like X hours or something.
But, here goes:

var dom = XDocument.DOM;
var t1 = dom.selectSingleNode("my:myFields/my:time1").nodeTypedValue;
var t2 = dom.selectSingleNode("my:myFields/my:time2").nodeTypedValue;
var t = null;

if (t1.match(/T(\d\d):(\d\d):(\d\d)/))
t1 = Number(RegExp.$1)*3600 + Number(RegExp.$2)*60 +
Number(RegExp.$3);
else
XDocument.UI.Alert("Time 1 is invalid.");

if (t2.match(/T(\d\d):(\d\d):(\d\d)/))
t2 = Number(RegExp.$1)*3600 + Number(RegExp.$2)*60 +
Number(RegExp.$3);
else
XDocument.UI.Alert("Time 2 is invalid.");

t = t1 + t2;

var h = Math.floor(t / 3600);
var m = Math.floor(t % 3600 / 60);
var s = t % 60;

t = String(h + ":" + m + ":" + s).replace(/\b(\d)\b/g, "0$1");

XDocument.UI.Alert(t);

This question has been asked in this newsgroup before, so you may find
additional information by doing a search of this group.
--josh bertsch
 
M

Matthew Blain \(Serriform\)

If you create a JScript Date object, you can subtract them--a Date object as
a number is an offset in milliseconds from a particular point in time, so
the difference between two Date objects will be the elapsed time.

--Matthew Blain
http://tips.serriform.com/
http://www.microsoft.com/mspress/books/7128.asp


Josh Bertsch said:
The Data Validation sample in the SDK has some examples of
calculations involving dates and times.

[from Steve VD]
Mathmatical operations involving two time values don't make much sense
IMHO. Usually it's a time +/- an offset like X hours or something.
But, here goes:

var dom = XDocument.DOM;
var t1 = dom.selectSingleNode("my:myFields/my:time1").nodeTypedValue;
var t2 = dom.selectSingleNode("my:myFields/my:time2").nodeTypedValue;
var t = null;

if (t1.match(/T(\d\d):(\d\d):(\d\d)/))
t1 = Number(RegExp.$1)*3600 + Number(RegExp.$2)*60 +
Number(RegExp.$3);
else
XDocument.UI.Alert("Time 1 is invalid.");

if (t2.match(/T(\d\d):(\d\d):(\d\d)/))
t2 = Number(RegExp.$1)*3600 + Number(RegExp.$2)*60 +
Number(RegExp.$3);
else
XDocument.UI.Alert("Time 2 is invalid.");

t = t1 + t2;

var h = Math.floor(t / 3600);
var m = Math.floor(t % 3600 / 60);
var s = t % 60;

t = String(h + ":" + m + ":" + s).replace(/\b(\d)\b/g, "0$1");

XDocument.UI.Alert(t);

This question has been asked in this newsgroup before, so you may find
additional information by doing a search of this group.
--josh bertsch

JDVarney said:
I have two date fields, one startTime, and the other endTime. The times
occur in the same day. I want to calculate the value of the differences as
hours, for example 3:30 PM - 2:00 PM = 1.5 hours. I can't find a way to
calculate this. I'm new to manipulation XML and JScript. Any help would be
appreciated.

Thanks,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top