时间戳转时间
//格式化时间
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//选定时区
formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
[formatter setTimeZone:timeZone];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//进行转换 [aDictionary objectForKey:@"posttime"]是时间戳
NSDate* date = [NSDate dateWithTimeIntervalSince1970:[[aDictionary objectForKey:@"posttime"] doubleValue]];
//字符串接收转换完成的时间
self.postTime = [formatter stringFromDate:date];
时间转化为时间戳
// 当前时间
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
// *1000 是精确到毫秒,不乘就是精确到秒
NSTimeInterval a=[date timeIntervalSince1970]*1000;
//转为字符型
NSString *timeString = [NSString stringWithFormat:@"%.0f", a];
得到当前时间
NSDate *now = [NSDate date];
NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponent = [calendar1 components:unitFlags fromDate:now];
int year =(int) [dateComponent year];
int month = (int) [dateComponent month];
int day = (int) [dateComponent day];
获得两个时间的时间差(从字符串)
NSDate *nowDate = [NSDate date];
NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.0";
// 截止时间字符串格式 这里我计算当前时间和2015年的时间差
NSString *expireDateStr = @"2015-05-09 00:00:00.0";
// 当前时间字符串格式
NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];
// 截止时间data格式
NSDate *expireDate = [dateFomatter dateFromString:expireDateStr];
// 当前时间data格式
nowDate = [dateFomatter dateFromString:nowDateStr];
// 当前日历
NSCalendar *calendar = [NSCalendar currentCalendar];
// 需要对比的时间数据
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate:expireDate toDate:nowDate options:0];
//dateCom.year
int monthday = (int)dateCom.month *31 + (int)dateCom.day + (int)dateCom.year*365+1;
int day1 = monthday/100;
int day2 = (monthday-day1*100)/10;
int day3 = monthday-day1*100-day2*10;
这里就可以得到具体的时间差了
//dateCom.month
//dateCom.day
//dateCom.hour
//dateCom.minute
//dateCom.second
获得两个时间的时间差(从标准格式)
比较两个NSDate相差多少小时
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
//两个时间相差多少秒
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
计算当前日期为星期几
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/BeiJing"]; [calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
获取本周周一和周末的日期
- (NSString *)getWeekTime
{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
// 获取今天是周几
NSInteger weekDay = [comp weekday];
// 获取几天是几号
NSInteger day = [comp day];
NSLog(@"%d----%d",weekDay,day);
// 计算当前日期和本周的星期一和星期天相差天数
long firstDiff,lastDiff;
// weekDay = 1;
if (weekDay == 1)
{
firstDiff = -6;
lastDiff = 0;
}
else
{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
NSLog(@"firstDiff: %ld lastDiff: %ld",firstDiff,lastDiff);
// 在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[firstDayComp setDay:day + firstDiff];
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:nowDate];
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM月dd日"];
NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
NSLog(@"%@=======%@",firstDay,lastDay);
NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];
return dateStr;
}