简体   繁体   English

Javascript日期/时间无法在使用Chrome的iOS设备上运行

[英]Javascript Date/Time not Working on iOS Device with Chrome

I am working with date/time in javascript and it is working properly on chrome windows but it is not working within Chrome on iOS devices. 我正在使用javascript中的日期/时间,它在Chrome窗口上正常工作,但它在iOS设备上的Chrome中无效。 The difference between two times is not calculated properly on iOS. 在iOS上无法正确计算两次之间的差异。

Here is my code: 这是我的代码:

var start_timeObj = moment(start_time, ["h:mm A"]);
var end_timeObj = moment(end_time, ["h:mm A"]);
var start_time=start_timeObj.format("HH:mm");
var end_time=end_timeObj.format("HH:mm"); 

var diff = ( new Date("1970-1-1 " + end_time) - new Date("1970-1-1 " + start_time) ) / 1000 / 60 / 60;

if(diff==1)
{

Have you tried using moment's built in diff functionality? 你尝试过使用moment内置的diff功能吗?

var start = moment(start_time, ["h:mm A"]);
var end = moment(end_time, ["h:mm A"]);
var diffMilliseconds = end.diff(start) // outputs 3600000 (in ms)
var diffHours = diff / 1000 / 60 / 60; // outputs 1

Documentation: https://momentjs.com/docs/#/displaying/difference/ 文档: https//momentjs.com/docs/#/displaying/difference/

If you don't want to use moment you could try using: 如果您不想使用片刻,可以尝试使用:

var date1 = new Date('1998-07-20');
var date2 = new Date('1998-07-23');
var diffTime = Math.abs(date2.getTime() - date1.getTime());
diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1; 

If you don't add 1 to diffDays the end date won't be included. 如果不向diffDays添加1 ,则不包括结束日期。

You can do exactly the same using a H:m:s format ( https://playcode.io/396986?tabs=script.js,preview,console ) 您可以使用H:m:s格式完成相同的操作( https://playcode.io/396986?tabs=script.js,preview,console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM