gpt4 book ai didi

ios - 我如何转换为特定时区?

转载 作者:行者123 更新时间:2023-11-28 06:47:09 27 4
gpt4 key购买 nike

这是一个工作函数,可以将分钟四舍五入到最接近的五。我需要的是它返回特定时区的时间,所以无论你在时区 ​​+1、+5 还是其他什么,它都必须返回时区 +1。任何人都知道我该如何解决这个问题?

func SkipToEvenFiveMinutes(date:NSDate) -> NSDate!
{

let componentMask : NSCalendarUnit = ([NSCalendarUnit.Year , NSCalendarUnit.Month , NSCalendarUnit.Day , NSCalendarUnit.Hour ,NSCalendarUnit.Minute, NSCalendarUnit.Second+NSTimeZone setDefaultTimeZone:UTC+1])
let components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)
let current = components.minute * 60
let currentSeconds = current + components.second

let diff = currentSeconds % (60 * 5);
let difference = Double(diff)
if (difference > 60.0 * 2.5) {
components.second += 60 * 5 - diff
} else {
components.second -= diff
}

return NSCalendar.currentCalendar().dateFromComponents(components)!

}

最佳答案

Dates does not have time zones.所以没有什么可以转换的。日历和格式化程序有时区。

因此,您应该做的是为不同的时区选择正确的日历。然后您可以使用第二个日历对象获取该时区的单位。

关于ios - 我如何转换为特定时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35987756/

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