如何将分钟添加到字符串日期
How to add minutes to string date
我有一个来自 API 的日期字符串,看起来像这样 10:00 我需要添加一个 1 和半,我试过这个但它不工作
moment('10:00').add(90, 'minute')
我期待这个输出 11:30
let time = "10:00"
const add=(minutes)=>{
let [hr, min] = time.split(":")
minutes += parseInt(hr)*60 + parseInt(min)
hr = Math.floor(minutes/60)
min = minutes %60
//console.log(hr%12+":"+ min)
return (hr%12+":"+ min)
}
console.log(add(90))
查看内联评论:
let input = "10:00";
let inputParts = input.split(":"); // Split the string at the colon
let d = new Date(); // Create new date
d.setUTCHours(+inputParts[0] + 1); // Get first part of string and add 1
d.setUTCMinutes(30); // Adjust by 1.5 minutes
// Output in the format you want
console.log(d.getUTCHours() + ":" + d.getUTCMinutes());
我有一个来自 API 的日期字符串,看起来像这样 10:00 我需要添加一个 1 和半,我试过这个但它不工作
moment('10:00').add(90, 'minute')
我期待这个输出 11:30
let time = "10:00"
const add=(minutes)=>{
let [hr, min] = time.split(":")
minutes += parseInt(hr)*60 + parseInt(min)
hr = Math.floor(minutes/60)
min = minutes %60
//console.log(hr%12+":"+ min)
return (hr%12+":"+ min)
}
console.log(add(90))
查看内联评论:
let input = "10:00";
let inputParts = input.split(":"); // Split the string at the colon
let d = new Date(); // Create new date
d.setUTCHours(+inputParts[0] + 1); // Get first part of string and add 1
d.setUTCMinutes(30); // Adjust by 1.5 minutes
// Output in the format you want
console.log(d.getUTCHours() + ":" + d.getUTCMinutes());