時間戳和當前時間轉換

1.把獲取的時間轉換成當前時間code

NSDate *dateNow = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:dateNow];
    NSDate *localDate = [dateNow dateByAddingTimeInterval:interval];
    NSLog(@"%@",localDate);

2.把當前時間轉換成時間戳orm

_timeSp = (long)[dateNow timeIntervalSince1970];
    NSLog(@"timeSp:%ld",(long)_timeSp); //時間戳的值

3.把時間戳轉換爲時間string

NSDate *time = [NSDate dateWithTimeIntervalSinceNow:1464274800];
    NSLog(@"%@",time);

4.輸出只有年月日   忽略時分秒it

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *str = [formatter stringFromDate:date];
相關文章
相關標籤/搜索