希望在javascript中一起添加时间。
我从来没有写过任何javascript,因此我正在努力进步。
我必须将4:00
(4小时)添加到12:44
(当前时间)这是否可以在javascript中使用?
答案应该报告16:44
如果是这样,我该怎么做呢?
由于
答案 0 :(得分:6)
看看moment.js - 一个用于管理各种时间相关功能的出色库 - momentjs.com
后来回答:
你提到你是一个使用JavaScript的新手,所以这里是一个使用moment.js的问题的简单工作示例 - 这个例子假设文件和moment.js在同一个文件夹中。查看关于所有格式选项的moment.js上的文档。祝你好运。
<!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>
答案 1 :(得分:4)
如果你把它分解成几个小助手函数,那就不太难了:
// Convert a time in hh:mm format to minutes
function timeToMins(time) {
var b = time.split(':');
return b[0]*60 + +b[1];
}
// Convert minutes to a time in format hh:mm
// Returned value is in range 00 to 24 hrs
function timeFromMins(mins) {
function z(n){return (n<10? '0':'') + n;}
var h = (mins/60 |0) % 24;
var m = mins % 60;
return z(h) + ':' + z(m);
}
// Add two times in hh:mm format
function addTimes(t0, t1) {
return timeFromMins(timeToMins(t0) + timeToMins(t1));
}
console.log(addTimes('12:13', '01:42')); // 13:55
console.log(addTimes('12:13', '13:42')); // 01:55
console.log(addTimes('02:43', '03:42')); // 06:25
答案 2 :(得分:0)
使用CLUSTERED
可以将create table tbl_sales_Invoice_info (
id int Identity(1,1) not null,
InvoiceId Nvarchar(50) Not Null,
InvoiceDate date null,
Customer_id int null,
Grand_Total float(53) null,
Total_paid float(53) Null,
Balance Float(53),
primary key ([InvoiceId])
);
转换为分钟并添加它们。
示例:
moment.js
结果:hh:mm
因此,您可以将分钟转换为moment.duration("02:45").asMinutes() + moment.duration("02:15").asMinutes()
:
300 mins
答案 3 :(得分:0)
用于添加时间格式的字符串数组(包括秒,不包括天)。
例如:
输入:
times = ['00:00:10', '00:24:00']
输出
00:24:10
// Add two times in hh:mm:ss format
function addTimes(times = []) {
const z = (n) => (n < 10 ? '0' : '') + n;
let hour = 0
let minute = 0
let second = 0
for (const time of times) {
const splited = time.split(':');
hour += parseInt(splited[0]);
minute += parseInt(splited[1])
second += parseInt(splited[2])
}
const seconds = second % 60
const minutes = parseInt(minute % 60) + parseInt(second / 60)
const hours = hour + parseInt(minute / 60)
return z(hours) + ':' + z(minutes) + ':' + z(seconds)
}