栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript中日期之间的差异

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript中日期之间的差异

通过使用Date对象及其毫秒值,可以计算出差异:

var a = new Date(); // Current date now.var b = new Date(2010, 0, 1, 0, 0, 0, 0); // Start of 2010.var d = (b-a); // Difference in milliseconds.

您可以通过将毫秒除以1000来将秒转换为秒,然后将结果转换为整数来获得秒数(作为整数/整数):(除去代表毫秒的小数部分):

var seconds = parseInt((b-a)/1000);

然后可以

minutes
通过除以
seconds
60并将其转换为整数,然后
hours
除以
minutes
60并将其转换为整数,然后以相同的方式获得更长的时间单位来获得整数。由此,可以创建一个功能,以时间单位的最大值获得一个较低的单位,其余的较低的单位:

function get_whole_values(base_value, time_fractions) {    time_data = [base_value];    for (i = 0; i < time_fractions.length; i++) {        time_data.push(parseInt(time_data[i]/time_fractions[i]));        time_data[i] = time_data[i] % time_fractions[i];    }; return time_data;};// Input parameters below: base value of 72000 milliseconds, time fractions are// 1000 (amount of milliseconds in a second) and 60 (amount of seconds in a minute). console.log(get_whole_values(72000, [1000, 60]));// -> [0,12,1] # 0 whole milliseconds, 12 whole seconds, 1 whole minute.

如果您想知道上面为第二个Date对象提供的输入参数是什么,请在下面查看它们的名称:

new Date(<year>, <month>, <day>, <hours>, <minutes>, <seconds>, <milliseconds>);

如该解决方案的注释中所述,除非您希望代表的日期是必需的,否则您不必提供所有这些值。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号