一起添加2次JavaScript [英] Add 2 times together JavaScript

查看:108
本文介绍了一起添加2次JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在javascript中一起添加时间。

Looking to add time together within javascript.

我从来没有写过任何javascript,因此我正在努力进步。

I have never written any javascript, and as such am struggling with the way to progress.

我必须将 4:00 (4小时)添加到 12: 44 (当前时间)这是否可以在javascript中使用?

I have got to add say 4:00 (4 hours) to 12:44 (Current Time) Is this possible within javascript?

答案应该报告 16:44

如果是这样,我该怎么办呢?

If so, how would I go about it?

谢谢

推荐答案

看看moment.js - 一个管理各种时间相关功能的优秀图书馆 - momentjs.com

Take a look at moment.js - an excellent library for managing all kinds of time related functionality - momentjs.com

后来添加回答:

你提到你是一个使用JavaScript的新手,所以这里是一个使用moment.js的问题的简单工作示例 - 这个例子假设文件和moment.js在同一个文件夹中。查看关于所有格式选项的moment.js上的文档。祝你好运。

You mention your are a newbie with JavaScript so here is a simple working example of your problem using moment.js - this example assumes the file and moment.js are in the same folder. Check out the docs on the moment.js for all the formatting options. Good luck.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Time</title>
<script src="moment.js"></script>
</head>

<body>

   <script>

   //add 4 hours to the stated time
   var theFutureTime = moment().hour('12').minute('44').add(4,'hours').format("HH:mm");

   console.log(theFutureTime);  // prints 16:44

  </script>

</body>

这篇关于一起添加2次JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆