基本上,就像标题说的那样。我想知道如何添加1天的NSDate。
如果它是:
21st February 2011
它将变成:
22nd February 2011
或者如果它是:
31st December 2011
它将变成:
1st January 2012.
基本上,就像标题说的那样。我想知道如何添加1天的NSDate。
如果它是:
21st February 2011
它将变成:
22nd February 2011
或者如果它是:
31st December 2011
它将变成:
1st January 2012.
当前回答
字符串扩展:转换String_Date >日期
extension String{
func DateConvert(oldFormat:String)->Date{ // format example: yyyy-MM-dd HH:mm:ss
let isoDate = self
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = oldFormat
return dateFormatter.date(from:isoDate)!
}
}
日期扩展:转换日期>字符串
extension Date{
func DateConvert(_ newFormat:String)-> String{
let formatter = DateFormatter()
formatter.dateFormat = newFormat
return formatter.string(from: self)
}
}
日期扩展:获取+/-日期
extension String{
func next(day:Int)->Date{
var dayComponent = DateComponents()
dayComponent.day = day
let theCalendar = Calendar.current
let nextDate = theCalendar.date(byAdding: dayComponent, to: Date())
return nextDate!
}
func past(day:Int)->Date{
var pastCount = day
if(pastCount>0){
pastCount = day * -1
}
var dayComponent = DateComponents()
dayComponent.day = pastCount
let theCalendar = Calendar.current
let nextDate = theCalendar.date(byAdding: dayComponent, to: Date())
return nextDate!
}
}
用法:
let today = Date()
let todayString = "2020-02-02 23:00:00"
let newDate = today.DateConvert("yyyy-MM-dd HH:mm:ss") //2020-02-02 23:00:00
let newToday = todayString.DateConvert(oldFormat: "yyyy-MM-dd HH:mm:ss")//2020-02-02
let newDatePlus = today.next(day: 1)//2020-02-03 23:00:00
let newDateMinus = today.past(day: 1)//2020-02-01 23:00:00
参考:来自多重问题 我如何添加1天到一个NSDate? 数学函数转换正整数为负和负到正? 将NSString转换为NSDate(然后再转换回来)
其他回答
斯威夫特5
let today = Date()
let nextDate = Calendar.current.date(byAdding: .day, value: 1, to: today)
objective - c
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// now build a NSDate object for the next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: [NSDate date] options:0];
只是为了好玩,通过一些扩展和操作符重载,你可以得到一些不错的东西,比如:
let today = Date()
let tomorrow = today + 1.days
, or
var date = Date()
date += 1.months
下面是支持代码:
extension Calendar {
struct ComponentWithValue {
let component: Component
let value: Int
}
}
extension Int {
var days: Calendar.ComponentWithValue {
.init(component: .day, value: self)
}
var months: Calendar.ComponentWithValue {
.init(component: .month, value: self)
}
}
func +(_ date: Date, _ amount: Calendar.ComponentWithValue) -> Date {
Calendar.current.date(byAdding: amount.component, value: amount.value, to: date)!
}
func +(_ amount: Calendar.ComponentWithValue, _ date: Date) -> Date {
date + amount
}
func +=(_ date: inout Date, _ amount: Calendar.ComponentWithValue) {
date = date + amount
}
代码是最少的,并且可以很容易地扩展到允许.月,.年,.小时等。还可以无缝添加对减法(-)的支持。
虽然在+操作符的实现中有一个强制的展开,但是不确定在哪种情况下日历可以返回nil日期。
斯威夫特2.0
let today = NSDate()
let calendar = NSCalendar.currentCalendar()
let tomorrow = calendar.dateByAddingUnit(.Day, value: 1, toDate: today, options: NSCalendarOptions.MatchFirst)
从iOS 8开始,你可以使用NSCalendar.dateByAddingUnit
Swift 1.x中的示例:
let today = NSDate()
let tomorrow = NSCalendar.currentCalendar()
.dateByAddingUnit(
.CalendarUnitDay,
value: 1,
toDate: today,
options: NSCalendarOptions(0)
)
斯威夫特2.0:
let today = NSDate()
let tomorrow = NSCalendar.currentCalendar()
.dateByAddingUnit(
.Day,
value: 1,
toDate: today,
options: []
)
斯威夫特3.0:
let today = Date()
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)
简单的Swift扩展昨天和明天从任何日期:
extension Date {
var previousDay: Date {
Calendar.current.date(byAdding: DateComponents(day:-1), to: self)!
}
var nextDay: Date {
Calendar.current.date(byAdding: DateComponents(day:+1), to: self)!
}
}
我在这里根据问题中的建议强制展开可选选项: 何时dateByAddingComponents:toDate:options返回nil?