如何在一天中增加24小时(例如74 + 24小时)使其在JavaScript中变为75

     let day = queryResults[0].day;
     //let day = 74 
    var current = new Date(); 
    var followingDay =day + 24 hours  ; 
    console.log(followingDay)

现在第一次是74天,我想得到74 + 1天,请帮助。

评论
佑佴玎
佑佴玎

这是一个入门的示例,从本质上来说,JavaScript有点混乱:

const addHours = (hours) => {
  const now = new Date();

  return new Date(now.setHours(now.getHours() + hours));
};

console.log(addHours(24));

We're essentially repeating ourselves considerably, we create a new Date (now), we then return a new date, calculated from now.getHours() + hours.

In addition to creating a new Date instance, we also use now.setHours to update the time using the getHours method.

Whenever I begin to implement anything that deals with time and date, I always keep this video in my mind: https://www.youtube.com/watch?v=-5wpm-gesOY

这是一个电脑爱好者的视频,解释说有时(尤其是时间/日期),您可能会完成比原先想要的更多的工作。在大多数情况下,像Moment之类的库将为此类功能提供更丰富的API。

点赞
评论