7秒丶记忆吧 关注:2贴子:59
  • 1回复贴,共1
+(NSString *)getShowTime:(NSDate*)adate aTime:(BOOL)atime{
//added by yfCui
BOOL isCurYear = YES;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *compNow = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
now=[NSDate date];
compNow = [calendar components:unitFlags fromDate:now];
NSDateComponents *compDate = [[NSDateComponents alloc] init];
compDate = [calendar components:unitFlags fromDate:adate];
if(compDate.year != compNow.year)
{
isCurYear = NO;
}
//end
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//modified by yfCui in 2014-3-31
if(isCurYear)
{
dateFormatter.dateFormat = @"YYYY年MM月dd日 HH:mm:ss";
}
else
{
dateFormatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
}
NSString *dateString = [dateFormatter stringFromDate:adate];
NSString *strDate = nil;
NSString *strTime = nil;
if(isCurYear)
{
strTime = [dateString substringWithRange:NSMakeRange(12,5)];
}
else
{
strTime = [dateString substringWithRange:NSMakeRange(11,5)];
}
NSDate *logDate = adate;
NSTimeInterval timeInterval = [logDate timeIntervalSinceNow];
timeInterval = -timeInterval;
long temp = 0;
temp = timeInterval/60;
temp = temp/60;
if (temp >= 48)
{
if(isCurYear == YES)
{
if (temp>=48&&temp<=24*7){
// NSData *resultDate = [NSData dataWithBase64EncodedString:dateString];
strDate = [self weekdayStringFromDate:adate];
}else{
strDate = [dateString substringWithRange:NSMakeRange(5,6)];
if([strDate startWith:@"0"])
{
strDate = [strDate substringFromIndex:1];
}
NSArray *dayArray = [strDate componentsSeparatedByString:@"月"];
NSString *curDay = [dayArray objectAtIndex:1];
if([curDay startWith:@"0"])
{
curDay = [curDay substringFromIndex:1];
}
strDate = [NSString stringWithFormat:@"%@月%@",[dayArray objectAtIndex:0],curDay];
}
}
else
{
strDate = [dateString substringWithRange:NSMakeRange(0,10)];
NSArray *dateArray = [strDate componentsSeparatedByString:@"-"];
NSString *month = [dateArray objectAtIndex:1];
if([month startWith:@"0"])
{
month = [month substringFromIndex:1];
}
NSString *day = [dateArray objectAtIndex:2];
if([day startWith:@"0"])
{
day = [day substringFromIndex:1];
}
strDate = [NSString stringWithFormat:@"%@-%@-%@",[dateArray objectAtIndex:0],month,day];
NSString *year = [dateString substringWithRange:NSMakeRange(0,4)];
NSString *nowYear = [NSString stringWithFormat:@"%d",compNow.year];
if ([nowYear integerValue] - [year integerValue] == 1) {
strDate = [NSString stringWithFormat:@"去年%@-%@",month,day];
}
}
}
else if (temp >= 24 && temp <48)
{
NSString *strDay = [dateString substringWithRange:NSMakeRange(8,2)];
NSInteger nDay = [strDay intValue];
NSString *nowDateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *strNowDay = [nowDateString substringWithRange:NSMakeRange(8, 2)];
NSInteger nNowDay = [strNowDay intValue];
if (nNowDay < nDay)
{
strDate = @"昨天";
}
else
{
if (nNowDay-nDay ==1)
{
strDate = @"昨天";
}
else
{
strDate = [dateString substringWithRange:NSMakeRange(5,6)];
}
}
}
else
{
NSString *strDay = [dateString substringWithRange:NSMakeRange(8,2)];
NSString *nowDateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *strNowDay = [nowDateString substringWithRange:NSMakeRange(8, 2)];
if ([strDay isEqualToString:strNowDay])
{
strDate = @"今天";
}
else
{
strDate = @"昨天";
}
}
NSString *strShowTime = nil;
// if(btime)
// {
if(isCurYear)
{
if([strDate isEqualToString:@"今天"])
{
strShowTime = [NSString stringWithFormat:@"今天 %@",strTime];
}
else
{
strShowTime = [NSString stringWithFormat:@"%@ %@",strDate,strTime];
}
}
else
{
strShowTime = [NSString stringWithFormat:@"%@ %@",strDate,strTime];
}
// }
// else
// strShowTime = [NSString stringWithFormat:@"%@",strDate];
return strShowTime;
}


IP属地:山东1楼2015-10-19 10:36回复
    //获得系统时间
    NSDate * senddate=[NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString * locationString=[dateformatter stringFromDate:senddate];
    //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
    //NSString * morelocationString=[dateformatter stringFromDate:senddate];
    //获得系统日期
    NSCalendar * cal=[NSCalendar currentCalendar];
    NSUInteger unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year=[conponent year];
    NSInteger month=[conponent month];
    NSInteger day=[conponent day];
    NSString * nsDateString= [NSString stringWithFormat:@"%4d年%2d月%2d日",year,month,day];


    IP属地:山东2楼2015-10-19 10:37
    回复