在iOS中将日期转换为第二天的日期 [英] Convert date to next day's date in iOS

查看:215
本文介绍了在iOS中将日期转换为第二天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串格式的日期,就像 30-11-2012 一样。我希望它的旁边的日期再次像字符串格式 01-12-2012
有没有办法得到它?

I have a date in string format just like 30-11-2012. I want the date next to it like 01-12-2012 again in string format. Is there any way of getting it?

推荐答案

我得到了一个我忘了名字的答案,我是即将接受他/她的回答,但当我点击接受回答时,它告诉我帖子已被删除。

I got the answer by someone I forget the name, I was about to accept his/her answer but when I clicked on accept answer it told me that the post bas been removed.

该匿名人士给出的答案如下: / p>

The answer given by that anonymous person was given below

NSString *dateString = @"22-11-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];


NSDateComponents *components= [[NSDateComponents alloc] init];
[components setDay:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateIncremented= [calendar dateByAddingComponents:components toDate:dateFromString options:0];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *stringFromDate = [myDateFormatter stringFromDate:dateIncremented];
NSLog(@"%@", stringFromDate);

最后我真的要感谢那个匿名的人,我知道这不是那么棘手但是这对我帮助很大。再次感谢Anonymous Helper。

In the end I really like to thanks that anonymous person, I know this was not as much tricky but this helped me a lot. Thanks again you the Anonymous Helper.

这篇关于在iOS中将日期转换为第二天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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