如何向 NSDate 添加 1 天? [英] How do I add 1 day to an NSDate?

查看:24
本文介绍了如何向 NSDate 添加 1 天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,正如标题所说.我想知道如何为 NSDate 添加 1 天.

Basically, as the title says. I'm wondering how I could add 1 day to an NSDate.

如果是这样:

21st February 2011

它会变成:

22nd February 2011

或者如果是:

31st December 2011

它会变成:

1st January 2012.

推荐答案

Swift 5.0 :

var dayComponent    = DateComponents()
dayComponent.day    = 1 // For removing one day (yesterday): -1
let theCalendar     = Calendar.current
let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
print("nextDate : (nextDate)")

目标 C:

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

NSLog(@"nextDate: %@ ...", nextDate);

这应该是不言自明的.

这篇关于如何向 NSDate 添加 1 天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆