【问题标题】:Convert time string ("hours:minute") to date object将时间字符串(“小时:分钟”)转换为日期对象
【发布时间】:2020-06-10 14:54:09
【问题描述】:

这是带有小时和分钟的时间字符串(例如“03:37”)。我想更新那个时间的日期对象并在 ember JS 中创建日期对象。帮我解决这个问题。时间过去了 24 小时。

【问题讨论】:

    标签: javascript date time ember.js


    【解决方案1】:

    使用 substring() 函数提取小时和分钟。然后使用 setHours() 函数将它们分配给任何日期对象。

    const today = new Date();
    
    const timeString = "03:37";
    
    // Use the substring() function to extract hours and minutes
    const hours = timeString.substring(0,2);
    const minutes = timeString.substring(3,5);
    
    // Use the setHours() function to assign hours and minutes
    // to the "today" date object
    const modifiedDate = new Date(today.setHours(hours, minutes));
    
    console.log(modifiedDate);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2012-03-15
      • 2022-01-26
      • 2023-04-09
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多