如何向 NSDate 添加 1 天?

编程入门 行业动态 更新时间:2024-10-24 17:33:01
本文介绍了如何向 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 天?

本文发布于:2023-11-09 23:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NSDate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!