Swift 从当前日期中减去一定的天数,并以一定的日期格式打印
如何从当前日期中减去一定的天数(减去一天),并以特定的日期格式打印到控制台 我目前正在使用:Swift 从当前日期中减去一定的天数,并以一定的日期格式打印,swift,date,datetime,nsdateformatter,Swift,Date,Datetime,Nsdateformatter,如何从当前日期中减去一定的天数(减去一天),并以特定的日期格式打印到控制台 我目前正在使用: print((Calendar.current as NSCalendar).date(byAdding: .day, value: -1, to: Date(), options: [])!) 将日期和时间打印为: yyyy-MM-dd HH:MM:SS+0000 但我希望它像这样打印: 年月日 这有可能吗?最好把它分成几个更容易阅读/维护的行。首先,计算所需的日期,然后应用日期格式化程序 let
print((Calendar.current as NSCalendar).date(byAdding: .day, value: -1, to: Date(), options: [])!)
将日期和时间打印为:
yyyy-MM-dd HH:MM:SS+0000
但我希望它像这样打印:
年月日
这有可能吗?最好把它分成几个更容易阅读/维护的行。首先,计算所需的日期,然后应用日期格式化程序
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
print(dateFormatter.stringFromDate(yesterday))
最好将其分成几个更易于阅读/维护的行。首先,计算所需的日期,然后应用日期格式化程序
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
print(dateFormatter.stringFromDate(yesterday))
swift 3.0版
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
if let yesterday = yesterday {
print(dateFormatter.string(from: yesterday))
}else{
print("Date incorrectly manipulated")
}
swift 3.0版
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
if let yesterday = yesterday {
print(dateFormatter.string(from: yesterday))
}else{
print("Date incorrectly manipulated")
}
对于
日期
格式字符串,不应使用固定格式。因为你可能有来自世界各地的用户,他们看日期的方式不一样
相反,您应该使用模板格式。您只需指定要显示的组件及其顺序,如:
let dateFormatter = DateFormatter()
// dateFormatter.locale = Locale(identifier: "bn-BD") // Enable this line only at the time of your debugging/testing
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ddMMyyyy",
options: 0,
locale: dateFormatter.locale)
let date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
if let date = date {
let dateString = dateFormatter.string(from: date)
print(dateString)
}
你不应该自己设置你的语言环境。这是由警察做的
框架您只能在此时手动设置语言环境
测试你的应用程序
在上面的例子中,我使用Locale
作为的“bn BD”
,意思是孟加拉国的孟加拉语。您可以在区域设置列表中找到您的设置,您不应该对日期字符串使用固定格式。因为你可能有来自世界各地的用户,他们看日期的方式不一样
相反,您应该使用模板格式。您只需指定要显示的组件及其顺序,如:
let dateFormatter = DateFormatter()
// dateFormatter.locale = Locale(identifier: "bn-BD") // Enable this line only at the time of your debugging/testing
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "ddMMyyyy",
options: 0,
locale: dateFormatter.locale)
let date = Calendar.current.date(byAdding: .day, value: -1, to: Date())
if let date = date {
let dateString = dateFormatter.string(from: date)
print(dateString)
}
你不应该自己设置你的语言环境。这是由警察做的
框架您只能在此时手动设置语言环境
测试你的应用程序
在上面的例子中,我使用Locale
作为的“bn BD”
,意思是孟加拉国的孟加拉语。您可以在区域设置列表中找到您的应用程序为什么要使用NSCalendar
?只需使用日历及其方法。为什么要使用NSCalendar
?只需使用日历及其方法。在Swift 3中应使用DateFormatter而不是NSDateFormatter在Swift 3中应使用DateFormatter而不是NSDateFormatter