gpt4 book ai didi

arrays - 为时间轴定义数组的更好方法,它可以更通用

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:17 24 4
gpt4 key购买 nike

我有一个字符串数组,如何才能比当前更好地定义它?它对我有用,但看起来有点奇怪。

var arrTimelineEntry = ["12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am","8 am", "9 am", "10 am", "11 am","12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"]

最佳答案

以下格式化程序适用于不同的区域设置和语言。

let calendar = Calendar.current
let date = Date() //choose date on which DST never happened

let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "jm", options: 0, locale: NSLocale.current)

let dates = (0...23).map { (hour) in
return calendar.date(bySettingHour: hour, minute: 0, second: 0, of: date)!
}

let timeLine = dates.map(formatter.string)

关于arrays - 为时间轴定义数组的更好方法,它可以更通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43495334/

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