1、根据年、月、日获取星期几
- (int)getWeekdayFromDate:(int)nYead andMonth:(int)nMonth andDay:(int)nDay
{
NSDateComponents *_comps = [[NSDateComponents alloc] init];
[_comps setDay:nDay];
[_comps setMonth:nMonth];
[_comps setYear:nYead];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *_date = [gregorian dateFromComponents:_comps];
NSDateComponents *weekdayComponents =
[gregorian components:NSWeekdayCalendarUnit fromDate:_date];
int _weekday = [weekdayComponents weekday];
return _weekday;
}
或
- (NSUInteger)getWeekdayFromDate:(NSDate*)date
{
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
components = [calendar components:unitFlags fromDate:date];
NSUInteger weekday = [components weekday];
return weekday;
}
2、根据月和日获取星座
-(NSString *)getAstroWithMonth:(int)m day:(int)d
{
NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
NSString *astroFormat = @"102123444543";
NSString *result;
if (m < 1 || m > 12 || d < 1 || d > 31)
{
return @"错误日期格式!";
}
if(m == 2 && d > 29)
{
return @"错误日期格式!!";
}
else if(m == 4 || m == 6 || m == 9 || m == 11)
{
if (d > 30)
{
return @"错误日期格式!!!";
}
}
result = [NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2 - (d < [[astroFormat substringWithRange:NSMakeRange((m - 1), 1)] intValue] - (-19))*2, 2)]];
return result;
}
3、计算两个日期之间相差几天几小时几分钟
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate * senddate=[NSDate date];
//结束时间
NSDate *endDate = [dateFormatter dateFromString:@"2014-6-24 00:00:00"];
//当前时间
NSDate *senderDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:senddate]];
//得到相差秒数
NSTimeInterval time=[endDate timeIntervalSinceDate:senderDate];
int days = ((int)time)/(3600*24);
int hours = ((int)time)%(3600*24)/3600;
int minute = ((int)time)%(3600*24)600/60;
if (days <= 0&&hours; <= 0&&minute; <= 0)
dateContent=@"0天0小时0分钟";
else
dateContent=[[NSString alloc] initWithFormat:@"%i天%i小时%i分钟",days,hours,minute];
4、获取当前时间
NSDate *now = [NSDate date];
NSLog(@”now date is: %@”, now);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
int year = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second];
NSLog(@”year is: %d”, year);
NSLog(@”month is: %d”, month);
NSLog(@”day is: %d”, day);
NSLog(@”hour is: %d”, hour);
NSLog(@”minute is: %d”, minute);
NSLog(@”second is: %d”, second);
注:根据互联网资料整理
posted on 2014-07-15 10:43
王海光 阅读(292)
评论(0) 编辑 收藏 引用 所属分类:
IOS