在Javascript中计算日期差异的最佳方法是什么 [英] What's the best way to calculate date difference in Javascript

查看:157
本文介绍了在Javascript中计算日期差异的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Javascript中执行一个函数,比如VisualBasic DateDiff。

I doing a function in Javascript like the VisualBasic DateDiff.

你给出了两个日期和返回的时间间隔(秒,分钟,天等......)

You give two dates and the returning time interval (Seconds, Minutes, Days, etc...)

DateDiff(ByVal Interval As Microsoft.VisualBasic.DateInterval, _
  ByVal Date1 As Date, ByVal Date2 As Date) as Long

那么计算Javascript日期差异的最佳方法是什么?

So what's the best way to calculate the difference of Javascript Dates?

推荐答案

使用日期对象像这样:

function DateDiff(var /*Date*/ date1, var /*Date*/ date2) {
    return date1.getTime() - date2.getTime();
}

这将返回两个日期之间的毫秒数差异。将它转换为秒,分钟,小时等不应该太困难。

This will return the number of milliseconds difference between the two dates. Converting it to seconds, minutes, hours etc. shouldn't be too difficult.

这篇关于在Javascript中计算日期差异的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆