2 つの時間の間の経過分数を必要とするアプリケーションがあります。例: 9:00 から 10:00 までは 60 分です。これは私が解決した解決策ですが、過度に冗長に思えます。私はもっとうまくやれるだろうか?
編集: リモート システムから YYYYMMDD および HHMM または HMM の形式で文字列を受け取ります。
// define a start time and end time as string
var start_time ="830";
var end_time ="1930";
// make sure we have complete 4 digit time
start_time = ('0' + start_time).slice(-4);
end_time = ('0' + end_time).slice(-4);
// extract start minutes and seconds
start_hour = start_time.substring(0,2);
start_minute = start_time.substring(2,4);
// extract end minutes and seconds
end_hour = end_time.substring(0,2);
end_minute = end_time.substring(2,4);
//get current date - it seems that we need a date a valid date to create a Date object
var now = new Date();
var year = now.getUTCFullYear();
var month = now.getUTCMonth();
var day = now.getDate();
// create Date object
var start_date_time_object = new Date(year,month,day,start_hour,start_minute);
var end_date_time_object = new Date(year,month,day,end_hour,end_minute);
// get number of minutes between start and end times
var start_minutes = (start_date_time_object.getHours() * 60) + start_date_time_object.getMinutes();
var end_minutes = (end_date_time_object.getHours() * 60) + end_date_time_object.getMinutes();
// calculate net time elapsed
var duration = end_minutes - start_minutes;
// display
console.log(start_minutes);
console.log(end_minutes);
console.log(duration);