gpt4 book ai didi

javascript - 获取时间(以毫秒为单位),对应自定义字符串

转载 作者:行者123 更新时间:2023-12-03 10:14:35 26 4
gpt4 key购买 nike

假设我有一个“事件”数组,其中包含“事件”对象,如下所示:

var events = [
{
name: 'event1',
start: '10.30',
end: '10.45'
},{
name: 'event2',
start: '11.00',
end: '12.00'
}
]

现在,我将有一个超时函数执行以下操作:

setTimeout(function() {
var currentTime = Date.now();

// how can i transform the start and end times from the events object in milliseconds?

var startEventOne = events[0].start // -> how can I transform the string 10.30 in milliseconds?

}, 1000);

问题是,如何获得相当于输入字符串的以毫秒为单位的时间?这可能吗?

最佳答案

嘿,

var timeArr, 
hours,
minutes,
milliseconds,
date;

timeArr = events[0].start.split('.');
hours = timeArr[0];
minutes timeArr[1];
date = new Date(0, 0, 0, hours, minutes, 0, 0);
milliseconds = date.getTime();
// This will return you the number of milliseconds

关于javascript - 获取时间(以毫秒为单位),对应自定义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29944337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com