日期字符串转化为年月日

网友投稿 784 2022-11-27

日期字符串转化为年月日

日期字符串转化为年月日

若你只是想把绝对日期字符串(至1970到现在的毫秒数)转化为年月日,时分秒,可以建立一个分类,使用系统函数自己转化。不需要我上篇文章说的完全自己实现日期转化函数。 注意: 服务器默认记路的日期字符串是精确的毫秒,苹果是默认精确到秒。 调用例子:

"%lld", model.startPoint.obtainTime*1000]; _dateLabel.text = [time dateFomatterStringWithMD];

NSString+Extension.h

#import @interface NSString (Extension)/** * 把时间戳格式化为 MM月dd日 EEE HH:mm 的格式 */- (NSString *)dateFomatterStringWithMDEHM;/** * 把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式 */- (NSString *)dateFomatterStringWithYMDHM;/** * 把时间戳格式化为 yyyy.MM.dd HH:mm 的格式 */- (NSString *)dateFomatterStringWithymdhm;/** * 把时间戳格式化为 MM月dd日 HH:mm 的格式 */- (NSString *)dateFomatterStringWithMDHM;/** * 把时间戳格式化为 HH:mm 的格式 */- (NSString *)dateFomatterStringWithHM;/** * 把时间戳格式化为今天(*日) HH:mm 的格式 */- (NSString *)dateFomatterStringWithDayHM;/** * 把时间戳格式化为 MM月dd日的格式 */- (NSString *)dateFomatterStringWithMD;/** * 把时间戳格式化为 yyyy-MM-dd 的格式 */- (NSString *)dateFomatterStringWithYMD;@end

NSString+Extension.m

#import "NSString+Extension.h"@implementation NSString (Extension)#pragma mark - 把时间戳格式化为 MM月dd日 EEE HH:mm 的格式- (NSString *)dateFomatterStringWithMDEHM;{ FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"MM月dd日 EEE HH:mm"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 yyyy年MM月dd日 HH:mm 的格式- (NSString *)dateFomatterStringWithYMDHM;{ FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 yyyy.MM.dd HH:mm 的格式- (NSString *)dateFomatterStringWithymdhm{ FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy.MM.dd HH:mm"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 yyyy的格式- (NSString *)dateFomatterStringWithy{ FLDDLogDebug(@"函数"); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 MM月dd日 HH:mm 的格式- (NSString *)dateFomatterStringWithMDHM { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"MM月dd日 HH:mm"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 MM月dd日的格式- (NSString *)dateFomatterStringWithMD { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"MM月dd日"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为 HH:mm 的格式- (NSString *)dateFomatterStringWithHM { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"HH:mm"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}#pragma mark - 把时间戳格式化为今天(*日) HH:mm 的格式- (NSString *)dateFomatterStringWithDayHM { long long nowTime = (long long)([[NSDate date] timeIntervalSince1970] * 1000); if([self longLongValue] < 100000) { return @"未知时间"; } NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if(ABS(nowTime - [self longLongValue]) > 3600000) { [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"dd日"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; NSString *dayStr = [formatter stringFromDate:confromTimesp]; formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"HH:mm"]; confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp]; return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr]; } else { [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"dd日"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; NSString *dayStr = [formatter stringFromDate:confromTimesp]; confromTimesp = [NSDate dateWithTimeIntervalSince1970:nowTime/1000]; NSString *todayStr = [formatter stringFromDate:confromTimesp]; if([dayStr isEqualToString: todayStr]) { formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"HH:mm"]; confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp]; return [[NSString alloc] initWithFormat:@"今天 %@",hourMinuteStr]; } else { formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"HH:mm"]; confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; NSString *hourMinuteStr = [formatter stringFromDate:confromTimesp]; return [[NSString alloc] initWithFormat:@"%@ %@",dayStr,hourMinuteStr]; } }}/** * 把时间戳格式化为 yyyy-MM-dd 的格式 */- (NSString *)dateFomatterStringWithYMD;{ if([self longLongValue] < 100000) { return @"未知时间"; } NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[self longLongValue]/1000]; return [formatter stringFromDate:confromTimesp];}@end

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JSPatch被停用了,也就是不能使用JSPatch第三方框架热修复了
下一篇:SpringBoot开发技巧启动时配置校验实现示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~