iOS-获取当前时间和时间戳 时间和时间戳互相转换

获取当前的时间 时间戳 时间和时间戳互相转换等
//当前时间 str类型
+ (NSString *)getCurrentTimeString{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *dateTime=[formatter stringFromDate:[NSDate date]];
    return dateTime;
}

//当前时间 date类型
+ (NSDate *)getCurrentTime{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *dateTime=[formatter stringFromDate:[NSDate date]];
    NSDate *date = [formatter dateFromString:dateTime];
    return date;
}

//获取当前时间戳字符串 10位
+ (NSString *)getTimestampSince1970
{
    NSDate *datenow = [NSDate date];//现在时间
    NSTimeInterval interval = [datenow timeIntervalSince1970];//13位的*1000
    NSString *timeSp = [NSString stringWithFormat:@"%.0f",interval];
    return timeSp;
}

//时间 转 时间戳
+(NSString *)dateConversionTimeStamp:(NSDate *)date
{
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]*1000];
    return timeSp;
}

//十三位时间戳字符串 转 时间
+(NSString *)timeStampConversionNSString:(NSString *)timeStamp
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStamp longLongValue]/1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}

//时间字符串 转 十位时间戳字符串
+(NSString *)timeChangetimeChuoNSStrings:(NSString *)timeStamp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateCurrentStr = [formatter dateFromString:timeStamp];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateCurrentStr timeIntervalSince1970]];
    return timeSp;
}

//十位时间戳字符串 转 指定时间
+ (NSString *)timeWithTimeIntervalString:(NSString*)string
{
    NSTimeInterval interval    =[string doubleValue] ;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *dateString       = [formatter stringFromDate: date];
    return dateString;
}

//字符串转时间
+(NSDate *)nsstringConversionNSDate:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *datestr = [dateFormatter dateFromString:dateStr];
    return datestr;
}

//时间字符串 转 十位时间戳字符串
+ (NSString *)timeAnytimeChuoNSStrings:(NSString *)timeStamp
{
    NSString *fStr = @"yyyy-MM-dd HH:mm:ss";
    
    if ([timeStamp containsString:@"."]){
        fStr = @"yyyy.MM.dd HH:mm:ss";
    }else if ([timeStamp containsString:@"年"]){
        fStr = @"yyyy年MM月dd日 HH:mm:ss";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:fStr];
    NSDate *dateCurrentStr = [formatter dateFromString:timeStamp];
    if (!dateCurrentStr) {
        //格式不对是显示当前时间
        return [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
    }
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateCurrentStr timeIntervalSince1970]];
    
    return timeSp;
}


/**
 *  时间戳转化时间
 *
 *  @param time 时间戳
 *
 *  @return 时间字符串
 */
+(NSString *)chatTime:(NSString *)time
{
    NSDate *dat = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *date = [formatter stringFromDate:dat];
    
    NSString *str = nil;
    NSDate *dat23e = [NSDate date];
    NSString *string = [NSString stringWithFormat:@"%@",dat23e];
    NSString *years = [string substringToIndex:10]; //年月日
    NSString *years2 = [date substringToIndex:10];
    NSString *timeString = [string substringFromIndex:11];
    NSString *timeString2 = [date substringFromIndex:11];
    NSString *timeSecond = [timeString substringToIndex:5];
    NSString *timeSecond2 = [timeString2 substringToIndex:5];
    
    if ([years isEqualToString:years2]) {
        if ([[timeSecond2 substringToIndex:4] isEqualToString:[timeSecond substringToIndex:4]]) {
            if ( ([[timeSecond2 substringFromIndex:4] intValue] - [[timeSecond2 substringFromIndex:4] intValue]) == 0) {
                str = nil;
            }
        }else{
            str = timeSecond2;
        }
    }else{
        if ([[years substringToIndex:4] intValue] - [[years2 substringToIndex:4] intValue] == 0) {
            NSString *s = [years2 substringFromIndex:5];
            NSString *monthsLast = [s substringToIndex:2];
            NSString *dayLast = [s substringFromIndex:3];
            str = [NSString stringWithFormat:@"%@月%@日",monthsLast,dayLast];
            
        }else{
            NSString *s = [years2 substringFromIndex:5];
            NSString *monthsLast = [s substringToIndex:2];
            NSString *dayLast = [s substringFromIndex:3];
            str = [NSString stringWithFormat:@"%@年%@月%@日",[years2 substringToIndex:4],monthsLast,dayLast];
            
        }
    }
    return str;
}

//日期对比
+ (int)compareOneDay:(NSDate *)currentDay withAnotherDay:(NSDate *)BaseDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *currentDayStr = [dateFormatter stringFromDate:currentDay];
    NSString *BaseDayStr = [dateFormatter stringFromDate:BaseDay];
    NSDate *dateA = [dateFormatter dateFromString:currentDayStr];
    NSDate *dateB = [dateFormatter dateFromString:BaseDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", currentDay, BaseDay);
    if (result == NSOrderedDescending)
    {
        //  NSLog(@"Date1  is in the future");
        //  NSLog(@"说明当前时间大于指定时间");
        return 1;
    }else if (result == NSOrderedAscending){
        // NSLog(@"Date1 is in the past");
       //  NSLog(@"说明当前时间小于指定时间");
        return -1;
    }else{
        // NSLog(@"Both dates are the same");
        // NSLog(@"说明当前时间与指定时间相同");
        return 0;
    }
}

end

你可能感兴趣的:(iOS-获取当前时间和时间戳 时间和时间戳互相转换)