zoukankan      html  css  js  c++  java
  • 封装用于解析NSDate的便利的类

    封装用于解析NSDate的便利的类

    此类可以从NSDate中解析出年份,月份,日期,时,分,秒,毫秒,足够用来做好多事情了,现提供源码如下:

    以下是核心的类:

    TimeInfo.h 与 TimeInfo.m

    //
    //  TimeInfo.h
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class HumanTimeInfo;
    
    @interface TimeInfo : NSObject
    
    + (HumanTimeInfo *)humanCanUnderstandFromDate:(NSDate *)date;
    
    @end
    //
    //  TimeInfo.m
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "TimeInfo.h"
    #import "HumanTimeInfo.h"
    
    static NSDateFormatter   *_dateFormatter;
    
    @implementation TimeInfo
    
    + (void)initialize {
        if (self == [TimeInfo class]) {
            _dateFormatter            = [[NSDateFormatter alloc] init];
            _dateFormatter.locale     = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
            _dateFormatter.timeZone   = [NSTimeZone timeZoneWithName:@"GMT"];
            _dateFormatter.dateFormat = @"yyyy:MM:dd:MMM:MMMM:HH:mm:ss:aa:EEE:EEEE:SSS"; // SSSS
        }
    }
    
    + (HumanTimeInfo *)humanCanUnderstandFromDate:(NSDate *)date {
        if (date != nil) {
            NSArray *timeInfoArray = [[_dateFormatter stringFromDate:date] componentsSeparatedByString:@":"];
            HumanTimeInfo *info    = [HumanTimeInfo new];
            info.year              = timeInfoArray[0];
            info.mounth            = timeInfoArray[1];
            info.day               = timeInfoArray[2];
            info.enMounth          = timeInfoArray[3];
            info.fullEnMounth      = timeInfoArray[4];
            info.hour              = timeInfoArray[5];
            info.min               = timeInfoArray[6];
            info.sec               = timeInfoArray[7];
            info.amPm              = timeInfoArray[8];
            info.enWeakday         = timeInfoArray[9];
            info.fullWeakday       = timeInfoArray[10];
            info.mSec              = timeInfoArray[11];
            
            return info;
        } else {
            return nil;
        }
    }
    
    @end

    人类能够理解的信息类:

    HumanTimeInfo.h 与 HumanTimeInfo.m

    //
    //  HumanTimeInfo.h
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface HumanTimeInfo : NSObject
    
    @property (nonatomic, strong) NSString *year;         // 2014
    @property (nonatomic, strong) NSString *mounth;       // 10
    @property (nonatomic, strong) NSString *day;          // 16
    @property (nonatomic, strong) NSString *enMounth;     // Oct
    @property (nonatomic, strong) NSString *fullEnMounth; // October
    @property (nonatomic, strong) NSString *hour;
    @property (nonatomic, strong) NSString *min;
    @property (nonatomic, strong) NSString *sec;
    @property (nonatomic, strong) NSString *amPm;         // 上午或者下午
    @property (nonatomic, strong) NSString *enWeakday;
    @property (nonatomic, strong) NSString *fullWeakday;
    @property (nonatomic, strong) NSString *mSec;         // 毫秒
    
    @end
    //
    //  HumanTimeInfo.m
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "HumanTimeInfo.h"
    
    @implementation HumanTimeInfo
    
    @end

    NSDate+CurrentTime.h 与 NSDate+CurrentTime.m

    //
    //  NSDate+CurrentTime.h
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    @class HumanTimeInfo;
    
    @interface NSDate (CurrentTime)
    
    + (HumanTimeInfo *)currentTime;
    + (HumanTimeInfo *)dateFrom:(NSDate *)date;
    
    @end
    //
    //  NSDate+CurrentTime.m
    //  ShowTime
    //
    //  Created by YouXianMing on 14-10-16.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "NSDate+CurrentTime.h"
    #import "HumanTimeInfo.h"
    #import "TimeInfo.h"
    
    @implementation NSDate (CurrentTime)
    
    + (HumanTimeInfo *)currentTime
    {
        return [TimeInfo humanCanUnderstandFromDate:[NSDate date]];
    }
    
    + (HumanTimeInfo *)dateFrom:(NSDate *)date
    {
        return [TimeInfo humanCanUnderstandFromDate:date];
    }
    
    @end

    使用的话,给出NSDate,然后解析出来,就是这么简单:)

  • 相关阅读:
    第一次实习项目总结
    javascript整理笔记(一)-----写项目的小技巧
    Vue(项目踩坑)_These dependencies were not found: * !!vue-style-loader!css-loader?{"sourceMap":true}!../../../node_modules/vue-loader/lib/style-compiler/index?{"vue"
    JS(递归-记一次面试题)-写一个get函数,get({a:1}, 'a')输出1,get({a : {b:2}},‘a.b’)输出2,按照此规律写一个函数
    html5_音视频的兼容性写法
    canvas_画出图片的马赛克
    项目(踩坑)_移动端在使用touch滑动事件的时候会出现抖动现象
    vue+mongoose+node.js项目总结第七篇_express框架中使用socket.io插件实现在线聊天功能(前端消息发送-后端接收逻辑)
    网址
    RAII Theory && auto_ptr
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4032014.html
Copyright © 2011-2022 走看看