关闭

根据时间戳显示时间日期,包括星期几。

79人阅读 评论(0) 收藏 举报

-(NSString*)showDate:(NSString*)dateStr

{

    NSLog(@"%@",[NSDate date]);

    

    //输入时间

    NSDate *inputdate = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]/1000];

    NSLog(@"%@",inputdate);

    

    NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];

    

    inputFormatter.timeZone= [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];

    

//    NSString*string = @"20151117121821";

//    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];

//    NSDate *inputdate=[inputFormatter dateFromString:string];

//    NSLog(@"%@",inputdate);


    [inputFormatter setDateFormat:@"yyyy"];

    NSString *nowyear=[inputFormatter stringFromDate:[NSDate date]];

    NSString *inputyear=[inputFormatter stringFromDate:inputdate];

    int yearDistance=[nowyear intValue]-[inputyear intValue];

    NSLog(@"%@-%@=%d", nowyear,inputyear,yearDistance);

    

    [inputFormatter setDateFormat:@"MM"];

    NSString *nowmonth=[inputFormatter stringFromDate:[NSDate date]];

    NSString *inputmonth=[inputFormatter stringFromDate:inputdate];

    int monthDistance=[nowmonth intValue]-[inputmonth intValue];

    NSLog(@"%@-%@=%d", nowmonth,inputmonth,monthDistance);

    

    [inputFormatter setDateFormat:@"dd"];

    NSString *nowday=[inputFormatter stringFromDate:[NSDate date]];

    NSString *inputday=[inputFormatter stringFromDate:inputdate];

    int dayDistance=[nowday intValue]-[inputday intValue];

    NSLog(@"%@-%@=%d", nowday,inputday,dayDistance);

    

    [inputFormatter setDateFormat:@"HH"];

    NSString *nowhour=[inputFormatter stringFromDate:[NSDate date]];

    NSString *inputhour=[inputFormatter stringFromDate:inputdate];

    int hourDistance=[nowhour intValue]-[inputhour intValue];

    NSLog(@"%@-%@=%d", nowhour,inputhour,hourDistance);

    

    NSString *date=nil;

    if (yearDistance==0 && monthDistance==0 && dayDistance==0 && hourDistance<=2) {

        

        [inputFormatter setDateFormat:@"HH:mm"];

        date=[inputFormatter stringFromDate:inputdate];

        

    }else if ((yearDistance==0 && monthDistance==0 && dayDistance==0 && hourDistance>2)){

        

        date=@"今天";

        

    }else if (yearDistance==0 && monthDistance==0 && dayDistance==1){

        

        date=@"昨天";

        

    }else if (yearDistance==0 && monthDistance==0 && dayDistance<=7){

        

        date=[NSString stringWithFormat:@"%@",[self weekdayStringFromDate:inputdate]];

        

    }else{

        

        [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

        date=[inputFormatter stringFromDate:inputdate];

    }

    NSLog(@"%@",date);

    return date;


}

-(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];

    

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档