Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用创建的日期计算当前日期的剩余时间?_Javascript_Momentjs - Fatal编程技术网

Javascript 如何使用创建的日期计算当前日期的剩余时间?

Javascript 如何使用创建的日期计算当前日期的剩余时间?,javascript,momentjs,Javascript,Momentjs,我需要计算到未来某一点的剩余时间。我正在使用new Date()获取当前日期,例如: Fri Oct 09 2020 09:59:21 GMT+0200 (Central European Summer Time) 如何计算对象中的剩余时间,直到该时间: { appointment_date: "Tue Oct 20 2020" appointment_hour: "08:00 - 09:00" } 我想以月、周、日等为单位显示。如果它们为

我需要计算到未来某一点的剩余时间。我正在使用
new Date()
获取当前日期,例如:

Fri Oct 09 2020 09:59:21 GMT+0200 (Central European Summer Time)
如何计算对象中的剩余时间,直到该时间:

{
   appointment_date: "Tue Oct 20 2020"
   appointment_hour: "08:00 - 09:00"
}
我想以月、周、日等为单位显示。如果它们为零,则应忽略它们。

您可以使用它

const约会={
任命日期:“2020年10月20日星期二”,
预约时间:“08:00-09:00”
};
const appointmentString=appointment.appointment\u date+“”+appointment.appointment\u hour.split('-')[0];
常数差=力矩。持续时间(
时刻(任命字符串,“ddd-MMM-DD-YYYY HH:mm”).diff(时刻());
常量结果=[];
常数单位=[
“月”,
“周”,
“天”,
“小时”,
“分钟”,
“秒”
];
单位。forEach(函数(单位){
//这等于差.months()>0,差.weeks()>0,以此类推
如果(差[单位]()>0){
结果.推送(差[单位]()+“”+单位);
}
});
console.log(result.join(',')
您可以使用它

const约会={
任命日期:“2020年10月20日星期二”,
预约时间:“08:00-09:00”
};
const appointmentString=appointment.appointment\u date+“”+appointment.appointment\u hour.split('-')[0];
常数差=力矩。持续时间(
时刻(任命字符串,“ddd-MMM-DD-YYYY HH:mm”).diff(时刻());
常量结果=[];
常数单位=[
“月”,
“周”,
“天”,
“小时”,
“分钟”,
“秒”
];
单位。forEach(函数(单位){
//这等于差.months()>0,差.weeks()>0,以此类推
如果(差[单位]()>0){
结果.推送(差[单位]()+“”+单位);
}
});
console.log(result.join(',')

如果您想要纯JavaScript的解决方案,请尝试以下方法

const appointment = {
  appointment_date: "Tue Oct 20 2020",
  appointment_hour: "08:00 - 09:00"
};

const result = [];
var curr = new Date().getTime();
var appo = new Date(appointment.appointment_date).getTime() + parseInt(appointment.appointment_hour.split(' - ')[0]) *60 *60 *1000;

function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = Math.floor( (t - d * cd) / ch),
        m = Math.round( (t - d * cd - h * ch) / 60000);
  if( m === 60 ){
    h++;
    m = 0;
  }
  if( h === 24 ){
    d++;
    h = 0;
  }
  return [d, h, m].join(':');
}
console.log(appo - curr)
console.log( dhm( appo - curr ) );

如果您想要纯JavaScript的解决方案,请尝试以下方法

const appointment = {
  appointment_date: "Tue Oct 20 2020",
  appointment_hour: "08:00 - 09:00"
};

const result = [];
var curr = new Date().getTime();
var appo = new Date(appointment.appointment_date).getTime() + parseInt(appointment.appointment_hour.split(' - ')[0]) *60 *60 *1000;

function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = Math.floor( (t - d * cd) / ch),
        m = Math.round( (t - d * cd - h * ch) / 60000);
  if( m === 60 ){
    h++;
    m = 0;
  }
  if( h === 24 ){
    d++;
    h = 0;
  }
  return [d, h, m].join(':');
}
console.log(appo - curr)
console.log( dhm( appo - curr ) );

谢谢鲁本的回复谢谢鲁本的回复