/** * GetMonthEndDate * 根据月头获取月尾日期 * * @param monthBegin 月头(yyyy-MM-dd HH:mm:ss) * * @return NSDate / nil */ +(NSDate *) GetMonthEndDate:(NSString *)monthBegin{ if (![monthBegin isEqualToString:@""]){ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC+8"]]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDateComponents *currentMonth = [[NSDateComponents alloc] init]; currentMonth.month = 1; NSDate *end = [[NSCalendar currentCalendar] dateByAddingComponents:currentMonth toDate:[formatter dateFromString:monthBegin] options:0]; return end; }else return nil; }
/** * GetWeekForDate * 根据日期获取星期 * * @param NSString strDate 日期(yyyy-MM-dd) * * @return NSString / empty */ +(NSString*) GetWeekForDate:(NSString *) strDate{ if (![strDate isEqualToString:@""]) { //时间格式化 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; //获取对应时间 [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *cdate = [formatter dateFromString:strDate]; //获取时间信息 [formatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm:ss a"]; NSString *locationString=[formatter stringFromDate: cdate]; NSArray *arrDate = [locationString componentsSeparatedByString:@" "]; NSString *strWeek = [NSString stringWithFormat:@"%@",[arrDate objectAtIndex:1]]; if ([strWeek isEqualToString:@"Sunday"]) return @"周末"; else if ([strWeek isEqualToString:@"Monday"]) return @"周一"; else if ([strWeek isEqualToString:@"Tuesday"]) return @"周二"; else if ([strWeek isEqualToString:@"Wednesday"])return @"周三"; else if ([strWeek isEqualToString:@"Thursday"]) return @"周四"; else if ([strWeek isEqualToString:@"Friday"]) return @"周五"; else if ([strWeek isEqualToString:@"Saturday"]) return @"周六"; else return @""; }else return nil; }
//获取时间戳 NSDate *datetime = [NSDate date]; NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:8]; NSInteger interval = [zone secondsFromGMTForDate:datetime]; NSDate *localeDate = [datetime dateByAddingTimeInterval: interval]; NSString *unixtime = [NSString stringWithFormat:@"%.f", floor([localeDate timeIntervalSince1970])];
http://book.2cto.com/201304/21439.html
http://blog.sina.com.cn/s/blog_4e24d9c501010lu2.html
原文地址:http://blog.csdn.net/yimiyuangguang/article/details/45305843