iOS 时间处理

2019-10-25  本文已影响0人  邓布利多教授

.h文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYDate : NSObject

/**
 按照格式返回时间
 */
+(NSString *)lyCustomDateTypeWithDate:(NSString *)date
                                 Type:(NSString *)type;

/**
 时间格式转时间戳
 */
+(NSInteger)lyTimestampWithDatetype:(NSString *)date
                               Type:(NSString *)type;

@end

NS_ASSUME_NONNULL_END

.m文件

#import "LYDate.h"

@implementation LYDate

+(NSString *)lyCustomDateTypeWithDate:(NSString *)date
                                 Type:(NSString *)type{
    
    //时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:type];
    //传入时间戳
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:[date intValue]];
    //返回结果
    return [dateFormatter stringFromDate:detaildate];
    
}

+(NSInteger)lyTimestampWithDatetype:(NSString *)date
                               Type:(NSString *)type{
    
    //时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //时间格式
    [formatter setDateFormat:type];
    //时间地区
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    //时间格式转成date
    NSDate* date = [formatter dateFromString:dateType];
    //时间转时间戳
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    return timeSp;
    
}

@end
上一篇下一篇

猜你喜欢

热点阅读