gpt4 book ai didi

javascript - 准备一个具有相应值的日期数组

转载 作者:行者123 更新时间:2023-12-03 05:22:08 25 4
gpt4 key购买 nike

0:{entry_counts: 2000, week_end_date: "2016-09-01"} 1:{entry_counts: 10000, week_end_date: "2016-09-08"} 2:{entry_counts: 20000, week_end_date: "2016-09 -22"} 3:{entry_counts: 40000, week_end_date: "2016-09-29"} 4:{entry_counts: 150000, week_end_date: "2016-10-13"}

最佳答案

由于您的日期不包含任何类型的时区甚至时间数据,因此它将被视为 UTC 时区。我们真的不需要时间来帮助我们解决这个问题。

您还在代码中使用了很长的引用,这使得代码更难以阅读并且更容易出错。 (例如:data.entryDetails[0].entry_counts[count].entry_counts;)我建议使用一些变量来减少这个问题。这还可以更轻松地检查该值是否有效甚至已定义。

当处理时间和增加时间时,valueOf 非常有用。我为你写了一个快速脚本。我确信可以通过一些错误检查并确保定义数据来改进它。除此之外,它应该对您有用!

节日快乐!

var countData    = [ {entry_counts: 2000, week_end_date: "2016-09-01"},{entry_counts: 10000, week_end_date: "2016-09-08"},{entry_counts: 20000, week_end_date: "2016-09-22"},{entry_counts: 40000, week_end_date: "2016-09-29"},{entry_counts: 150000, week_end_date: "2016-10-13"} ]
var today = ( new Date() ).valueOf()

var currentEntry = parseEntry( countData.shift() )
var nextEntry = parseEntry( countData.length ? countData.shift() : null )


var viewsArray = []
var datesArray = []

var time = currentEntry.dateValue

while( time <= today ){

// update currentEntry data used to the most up-to-date data we have
if( nextEntry && time > nextEntry.dateValue ) {

while( nextEntry && time > nextEntry.dateValue ){

currentEntry = nextEntry
nextEntry = parseEntry( countData.length ? countData.shift() : null )

}

}

viewsArray.push( currentEntry.views )
datesArray.push( new Date( time ) )

time += daysInMilliseconds(7)

}


console.log( viewsArray )


function parseEntry( entry ) {

if( !entry ) return null

entry.date = new Date( entry.week_end_date )
entry.dateValue = entry.date.valueOf()
entry.views = entry.entry_counts

return entry

}


function daysInMilliseconds( days ) {

return days * 24 * 60 * 60 * 1000

}

关于javascript - 准备一个具有相应值的日期数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330953/

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