如何使用jquery为时间字符串添加分钟和小时

pri*_*nce 4 javascript time jquery strtotime

我想在我的变量中添加30分钟然后一小时,我已经有了自己的日期

var initialDate = '10:00';
Run Code Online (Sandbox Code Playgroud)

所以

if (some condition){
    // i add 30 minutes ->10:30
}elseif(another condition){
    // i add 1hour ->11:00
}
Run Code Online (Sandbox Code Playgroud)

我尝试了这个但是没有用

var initialDate = '10:00';
var theAdd = new Date(initialDate);
var finalDate = theAdd.setMinutes(theAdd.getMinutes() + 30);
Run Code Online (Sandbox Code Playgroud)

sam*_*j90 10

如果我理解正确,以下内容对您有所帮助.

您需要通过脚本标记添加momentjs依赖项,您可以在JavaScript中解析,验证,操作和显示日期.

您可以在momentjs网站上找到更多相关文档

console.log(moment.utc('10:00','hh:mm').add(1,'hour').format('hh:mm'));

console.log(moment.utc('10:00','hh:mm').add(30,'minutes').format('hh:mm'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


Ju *_*ira 5

var theAdd = new Date();

// Set Hours, minutes, secons and miliseconds
theAdd.setHours(10, 00, 00, 000);

if (some condition) {
   // add 30 minutes --> 10:30
   theAdd.setMinutes(theAdd.getMinutes() + 30);
}
elseif (some condition) {
   // add 1 hour --> 11:00
   theAdd.setHours(theAdd.getHours() + 1);
}
Run Code Online (Sandbox Code Playgroud)

然后打印var theAdd来获取日期和时间。

要获取恰好的时间:

theAdd.getHours() + ":" + theAdd.getMinutes();
Run Code Online (Sandbox Code Playgroud)