本文介绍了如何唤醒iPhone的最后一个月份日期作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在iphone应用程序中检索给定月份的最后一个日期。有人可以告诉我如何实现这一点?我在stackoverflow上看到一些解决方案,但是他们只是给了最后一个月的DAY。感谢。
更新:
+(NSString *)getLastDateOfMonth:(NSString *)monthYearString {
// NSString * inputDate = [NSString stringWithFormat:@%@%@,@01 ,monthYearString];
NSString * inputDate = [NSString stringWithFormat:@%@%@,@01,@2012年5月];
NSLog(@输入日期: - %@,inputDate);
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@dd MMM yyyy];
NSDate * date = [dateFormatter dateFromString:inputDate];
NSCalendar * gregCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents * components = [gregCalendar组件:NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
NSInteger月= [组件月];
NSInteger year = [components year];
if(month == 12){
[components setYear:year + 1];
[components setMonth:1];
}
else {
[components setMonth:month + 1];
}
[components setDay:1];
NSDate * lastDate = [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];
NSDateFormatter * outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@MM / dd / yyyy];
NSString * lastDateOfMonth = [outputFormatter stringFromDate:lastDate];
return lastDateOfMonth;
}
如果我使用这个代码,在控制台中显示错误的输出如下
输入日期: - 2012年5月1日
LAST DATE月: - 06/01/2012
解决方案
您可以使用此功能: -
NSDate currDate = [NSDate date];
NSDate * getLastDateMonth(NSDate * currDate)
{
NSCalendar * gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents * components = [gregCalendar组件:NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currDate];
NSInteger月= [组件月];
NSInteger year = [components year];
if(month == 12){
[components setYear:year + 1];
[components setMonth:1];
}
else {
[components setMonth:month + 1];
}
[components setDay:1];
return [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];
}
I want to retrieve the last date of given month in iphone app. Can anybody tell how can I achieve this? I saw some solution on stackoverflow , but they just giving last DAY of month.
Thanks.
UPDATE:
+(NSString*)getLastDateOfMonth:(NSString *)monthYearString {
//NSString *inputDate = [NSString stringWithFormat:@"%@ %@",@"01",monthYearString];
NSString *inputDate = [NSString stringWithFormat:@"%@ %@",@"01",@"May 2012"];
NSLog(@"Input date:- %@",inputDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSDate *date = [dateFormatter dateFromString:inputDate];
NSCalendar *gregCalendar=[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components=[gregCalendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date];
NSInteger month=[components month];
NSInteger year=[components year];
if (month==12) {
[components setYear:year+1];
[components setMonth:1];
}
else {
[components setMonth:month+1];
}
[components setDay:1];
NSDate *lastDate = [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *lastDateOfMonth = [outputFormatter stringFromDate:lastDate];
return lastDateOfMonth;
}
If I use this code it showing me wrong output in console as follows
Input date:- 01 May 2012
LAst DAte in Month:- 06/01/2012
解决方案
You can do that by using this function :-
NSDate currDate=[NSDate date];
NSDate *getLastDateMonth(NSDate *currDate)
{
NSCalendar *gregCalendar=[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components=[gregCalendar components:NSMonthCalendarUnit|NSYearCalendarUnit fromDate:currDate];
NSInteger month=[components month];
NSInteger year=[components year];
if (month==12) {
[components setYear:year+1];
[components setMonth:1];
}
else {
[components setMonth:month+1];
}
[components setDay:1];
return [[gregCalendar dateFromComponents:components] dateByAddingTimeInterval:0];
}
这篇关于如何唤醒iPhone的最后一个月份日期作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!