gpt4 book ai didi

swift - 快速将 2 位数年份转换为 4 位数年份

转载 作者:行者123 更新时间:2023-11-30 13:36:13 25 4
gpt4 key购买 nike

我是 swift 新手,正在尝试一些日期格式。

我有一个 Int32 格式的 2 位数年份(15)

我想将其转换为 4 位数年份(2015),再次分配给 Int32 中的相同变量。

有人可以分享如何做到这一点吗?

我尝试了以下方法:

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "YY"
let convertedDate = dateFormatter.stringFromDate(expirationYear)
print(convertedDate)

但它给出错误“无法从 Int32 转换为 NSDate”

谢谢。

最佳答案

看看这是否有帮助:

let year = 2015
let dateString = "\(year)-03-16"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let date = formatter.dateFromString(dateString as String) {
print("\(date)")
}

关于swift - 快速将 2 位数年份转换为 4 位数年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047150/

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