日期字符串转化为年月日

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

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

NSString+Extension.h

#import <Foundation/Foundation.h>

@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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值