我有2个变量,其中学校开学时间的值和祷告持续时间以分钟为单位
var schoolStart = localStorage.getItem("start"); //9:58
var prayerduration = localStorage.getItem("prayer"); //10
我希望输出为10:08
如果开始时间为9:00
且持续时间为10,那么我希望输出为9:10
答案 0 :(得分:0)
建立你的另一个问题的答案,这是一个应该完成你需要的简单功能。它只是转换为分钟和返回,并检查分钟是否是一个数字,以便它显示时间,例如'10:8'
为您的例子:
function addMinutes(time, increment) {
var time = time.split(':');
var min = +time[0] * 60 + (+time[1])
min += increment;
minutes = String(min%60)
if(minutes.length === 1) {
minutes = '0' + minutes;
}
return String(Math.floor(min / 60)) + ':' + minutes;
}
console.log(addMinutes('9:58', 10))
答案 1 :(得分:0)
使用Moment.js
解析,验证,操作和显示JavaScript中的日期和时间。
var fixedFormat = 'hh:mm';
var currentDate = moment().format( fixedFormat );
console.log( currentDate );
localStorage.setItem("start", currentDate );
var prayerTime = '10';
localStorage.setItem("prayer", prayerTime );
var schoolStart = localStorage.getItem("start");
var prayerduration = localStorage.getItem("prayer");
console.log( schoolStart, ' : ', prayerduration );
var final = moment.utc( schoolStart, fixedFormat )
.add(prayerduration ,'minutes').format( fixedFormat );
console.log( final );
获取Current Time:
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
console.log( time );
var epochDate = new Date($.now()).getTime();
var date = new Date( epochDate ); // 1515502267978
console.log( date ); // Tue Jan 09 2018 18:20:43 GMT+0530 (India Standard Time)
答案 2 :(得分:0)
O(log n)