OC ----关于时间的处理

本文介绍了一种在iOS开发中处理时间的方法,包括如何获取当前时间距1970年的秒数,以及如何根据给定的秒数转换为指定格式的日期时间。提供了获取完整日期时间、仅时分秒及仅时分的实用函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

//获得当前时间距1970的秒数

- (NSString *)setCurrTimeLenght

{

    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];

    NSString *timeLenght = [NSString stringWithFormat:@"%0.0f",time];

    return timeLenght;

}

 

//根据距离1970年的秒数算给定秒数的日期时间

- (NSString *)getTimeString

{

    NSString *strTime = [[NSString alloc]initWithString:self];

    

    NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length >

([self setCurrTimeLenght].length+2)

 ? ([strTime longLongValue]/1000) : [strTime longLongValue]];

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

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  //时间格式

    

    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];

    strTime = [dateFormatter stringFromDate:mydate1970];

    

    return strTime;

}

 

//获取只有时分秒

- (NSString *)getHoursMinutesString

{

    NSString *strTime = [[NSString alloc]initWithString:self];

    NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length > ([self setCurrTimeLenght].length+2) ? ([strTime longLongValue]/1000) : [strTime longLongValue]];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];//@"yyyy-MM-dd HH:mm:ss"

    [dateFormatter setDateFormat:@"HH:mm:ss"];

    

    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];

    strTime = [dateFormatter stringFromDate:mydate1970];

    

    

    return strTime;

}

 

 

//获取时分

- (NSString *)getHoursMinString

{

    NSString *strTime = [[NSString alloc]initWithString:self];

    NSDate *mydate1970 = [NSDate dateWithTimeIntervalSince1970: strTime.length > ([self setCurrTimeLenght].length+2) ? ([strTime longLongValue]/1000) : [strTime longLongValue]];

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

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

    

    dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];

    strTime = [dateFormatter stringFromDate:mydate1970];

    

    return strTime;

 

}

转载于:https://www.cnblogs.com/qwer-BHS/p/5316143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值