时间戳转化时间和星期

 //时间戳转化时间(10位时间戳)
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970: 1446768000];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

//时间戳转化时间(13位时间戳)
NSString * timeStampString = [NSString stringWithFormat:@"%ld",self.videoModel.date];
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *currentTime = [formatter stringFromDate:date];
    NSLog(@"%@", currentTime);

[self GetTime];

-(void)GetTime
{

    NSString* string = @"2015-11-06 08:00";
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate* inputDate = [inputFormatter dateFromString:string];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitWeekday |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;

    comps = [calendar components:unitFlags fromDate:inputDate];

    NSInteger week = [comps weekday];
    NSString *strWeek = [self getweek:week];
    NSLog(@"week is:%@",strWeek);

}

-(NSString *)getweek:(NSInteger)week
{
    NSString *weekStr = nil;

    if(week == 1)
    {
        weekStr = @"星期天";

    } else if(week == 2) {

        weekStr = @"星期一";

    } else if(week == 3) {

        weekStr = @"星期二";

    } else if(week == 4) {

        weekStr = @"星期三";

    } else if(week == 5) {

        weekStr = @"星期四";

    } else if(week == 6) {

        weekStr=@"星期五";

    } else if(week == 7) {

        weekStr=@"星期六";
    }

    return weekStr;

}

你可能感兴趣的:(时间戳转化时间和星期)