NSDate

2019-07-07  本文已影响0人  一直很安静_25ae

NSDate的基本使用

// 创建一个时间对象
    NSDate *date = [NSDate date];
    // 打印出的时候是0时区的时间(北京-东8区)
    NSLog(@"%@", date);
    
    NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];
    
    
    // 从1970开始走过的秒数
    NSTimeInterval seconds = [date2 timeIntervalSince1970];
    
    // [date2 timeIntervalSinceNow];

//日期的格式化

void date2string()
{
    NSDate *date = [NSDate date];
    
    
    // 日期格式化类
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // y 年  M 月  d 日
    // m 分 s 秒  H (24)时  h(12)时
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *str = [formatter stringFromDate:date];
    
    NSLog(@"%@", str);
}

// @"2011/09/10 18:56"----> NSDate
//NSDateFormatter日期的格式化

int main()
{
    // 09/10/2011
    NSString *time = @"2011/09/10 18:56";
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy/MM/dd HH:mm";
    
    NSDate *date = [formatter dateFromString:time];
    NSLog(@"%@", date);
    return 0;
}

NSDate的常用方法

//将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间)
//NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000
//转化后:2018-03-27 14:54:41 +0000

-(NSDate *)getLocalDateFromUTCDate:(NSDate *)UTCDate{    
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    NSInteger seconds = [tz secondsFromGMTForDate: UTCDate];
    return [NSDate dateWithTimeInterval: seconds sinceDate: UTCDate];
}

//将当地时区的标准Date转化为UTCDate
//当前当地的标准时间:2018-03-27 14:54:41 +0000
//转化为世界标准时间:2018-03-27 06:54:41 +0000

-(NSDate *)getUTCDateFromLocalDate:(NSDate *)LocalDate{
    
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    NSInteger seconds = -[tz secondsFromGMTForDate: LocalDate];
    return [NSDate dateWithTimeInterval: seconds sinceDate: LocalDate];
    
}

//根据UTCDate获取当前时间字符串(钟表上显示的时间)
//输入:[NSDate date] 2018-03-27 07:44:05 +0000
//输出:2018-03-27 15:44:05

-(NSString *)localStringFromUTCDate:(NSDate *)UTCDate{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    [dateFormatter setTimeZone:tz];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString* result=[dateFormatter stringFromDate:UTCDate];
    return result;
    
}

//根据UTC字符串获取当前时间字符串(钟表上显示的时间)
//输入:2018-03-27 07:44:05
//输出:2018-03-27 15:44:05

-(NSString *)localStringFromUTCString:(NSString *)UTCString{
    
    //先将UTC字符串转为UTCDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:tz];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *UTCDate = [dateFormatter dateFromString:UTCString];
    
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSString* result = [dateFormatter stringFromDate:UTCDate];
    return result;
}

//将当前时间字符串转为UTCDate

-(NSDate *)UTCDateFromLocalString:(NSString *)localString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:localString];
    return date;
}

//将当前时间字符串转为UTC字符串

-(NSString *)UTCStringFromLocalString:(NSString *)localString{
    
    NSDate *date = [self UTCDateFromLocalString:localString];
    NSString *string = [NSString stringWithFormat:@"%@",date];
    NSString *result = [string substringToIndex:string.length-6];
    return result;
    
}

//UTCDate转UTC字符串

-(NSString *)UTCStringFromUTCDate:(NSDate *)UTCDate{
    
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc]init];
    [dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dataFormatter setTimeZone:tz];
    NSString *UTCString = [dataFormatter stringFromDate:UTCDate];
    return UTCString;
    
}

//将当前时间(UTCDate)转为时间戳

-(NSString *)timeStampFromUTCDate:(NSDate *)UTCDate{
    
    NSTimeInterval timeInterval = [UTCDate timeIntervalSince1970];
    // *1000,是精确到毫秒;这里是精确到秒;
    NSString *result = [NSString stringWithFormat:@"%.0f",timeInterval];
    return result;
    
}

//当前时间字符串(钟表上显示的时间)转为时间戳

-(NSString *)timeStamapFromLocalString:(NSString *)localString{
    
    //先转为UTCDate
    NSDate *UTCDate = [self UTCDateFromLocalString:localString];
    NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
    return timeStamap;
    
}

//将UTCString转为时间戳

-(NSString *)timeStamapFromUTCString:(NSString *)UTCString{
    
    //先将UTC字符串转为UTCDate;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *tz = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:tz];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *UTCDate = [dateFormatter dateFromString:UTCString];
    
    NSString *timeStamap = [self timeStampFromUTCDate:UTCDate];
    return timeStamap;
}

//时间戳转UTCDate

-(NSDate *)UTCDateFromTimeStamap:(NSString *)timeStamap{
    
    NSTimeInterval timeInterval=[timeStamap doubleValue];
    //  /1000;传入的时间戳timeStamap如果是精确到毫秒的记得要/1000
    NSDate *UTCDate=[NSDate dateWithTimeIntervalSince1970:timeInterval];
    return UTCDate;
    
}
上一篇下一篇

猜你喜欢

热点阅读