开发者

Need ASP or Javascript Function to get Hours

开发者 https://www.devze.com 2023-02-15 17:46 出处:网络
I need a function to get hours from开发者_如何转开发 two variable (date+time) var a date +time

I need a function to get hours from开发者_如何转开发 two variable (date+time)

var a date +time

var b date +time

result = a-b in hours


in ASP VBScript, the DateDiff function will do business

result = DateDiff('h', a, b)


In javascript, use the UTC() function. It will return the milliseconds since 1/1/1970. Get the difference between the to values and convert to hours.


The javascript Date object internally stores dates as integers, counting the number of milliseconds since 0:00 on January 1st, 1970.

Thus, adding/subtracting two JS dates from each others will result in the difference between the two, in millisconds. Converting those to hours is easy when you know there’s 60 * 60 * 1000 = 3 600 000 ms in one hour:

var a = new Date(old),
    b = new Date(newer),
    hoursBetween = (b - a) / 3600000;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号