NSDate在几天内倒计时

编程入门 行业动态 更新时间:2024-10-28 06:30:26
本文介绍了NSDate在几天内倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想创建一个截止日期的天数。我对几小时或几分钟不感兴趣,只是从当前日期到设定日期的天数。这就是我一直在尝试的:

I want to create a countdown of the number of days until a date. I'm not interested in hours or minutes, just the number of days from the current date until the set date. This is what I have been trying:

NSDate *date = [NSDate date]; NSString *dateString = [date description]; // this **dateString** string will have **"yyyy-MM-dd HH:mm:ss +0530"** // NSMutableString *mutString = dateString; NSArray *arr = [dateString componentSeperatedByString:@" "]; // arr will have [0] -> yyyy-MM-dd, [1] -> HH:mm:ss, [2] -> +0530 (time zone)

但它不起作用,因为当前日期是在几分钟内给出时间和秒。

But it doesn't work because the current date is giving time in minutes and seconds.

//NSDate *currentDate = [[NSDate alloc] init]; NSString *start = [arr objectAtIndex:0];//[NSString stringWithFormat:@"%@", currentDate];//@"2011-11-05"; NSLog(@"%@",start); NSString *end = @"2011-6-03"; NSDateFormatter *f = [[NSDateFormatter alloc] init]; [f setDateFormat:@"yyyy-MM-dd"]; NSDate *startDate = [f dateFromString:start]; NSDate *endDate = [f dateFromString:end]; [f release]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0]; NSInteger days = [components day]; [gregorianCalendar release]; NSLog(@"%i", days);

我真的不太了解这段代码,我不熟悉NSDate和格式化程序,但这是我的一堆互联网代码的组合。

I don't really understand much of this code, I'm not familar with NSDate and formatter, but this is my combination of a bunch of internet code.

提前致谢。

推荐答案

你可以用这种方式计算当天的天数....

You can count the number of days from current day by this way....

NSDate NewDate =这里比较你的比较日期;

NSDate NewDate = Here Comes your compare date;

NSDateComponents * dateComp = [[NSDateComponents alloc] init];

NSDateComponents *dateComp = [[NSDateComponents alloc] init];

NSCalendar *Calander = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormat=[[NSDateFormatter alloc] init]; NSDateComponents *comps=[[NSDateComponents alloc] init]; unsigned int unitFlags = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; dateComp = [Calander components:unitFlags fromDate:[NSDate date]]; [dateFormat setDateFormat:@"dd"]; [comps setDay:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"MM"]; [comps setMonth:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"yyyy"]; [comps setYear:[[dateFormat stringFromDate:[NSDate date]] intValue]]; [dateFormat setDateFormat:@"HH"]; [comps setHour:05]; [dateFormat setDateFormat:@"mm"]; [comps setMinute:30]; NSDate *currentDate=[Calander dateFromComponents:comps]; //NSLog(@"Current Date is :- '%@'",currentDate); dateComp = [Calander components:unitFlags fromDate:newDate]; [dateFormat setDateFormat:@"dd"]; [comps setDay:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"MM"]; [comps setMonth:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"yyyy"]; [comps setYear:[[dateFormat stringFromDate:newDate] intValue]]; [dateFormat setDateFormat:@"HH"]; [comps setHour:05]; [dateFormat setDateFormat:@"mm"]; [comps setMinute:30]; NSDate *reminderDate=[Calander dateFromComponents:comps]; //NSLog(@"Current Date is :- '%@'",reminderDate); //NSLog(@"Current Date is :- '%@'",currentDate); NSTimeInterval ti = [reminderDate timeIntervalSinceDate:currentDate]; //NSLog(@"Time Interval is :- '%f'",ti); int days = ti/86400;

更多推荐

NSDate在几天内倒计时

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

发布评论

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

>www.elefans.com

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