便利的获取系统的时分秒

便利的获取系统的时分秒

源码如下:

GlobalNormalTime.h 与 GlobalNormalTime.m

//
//  GlobalNormalTime.h
//  YouXianMingClock
//
//  Created by YouXianMing on 14-10-12.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface GlobalNormalTime : NSObject

/**
 *  当前时间的数组
 *
 *  @return 返回有3个元素的数组(0处为小时,1处为分钟,2处为秒)
 */
+ (NSArray *)currentTime;

/**
 *  当前秒
 *
 *  @return 当前秒
 */
+ (float)currentSecond;

/**
 *  当前分钟
 *
 *  @return 当前分钟
 */
+ (float)currentMinute;

/**
 *  当前小时
 *
 *  @return 当前小时
 */
+ (float)currentHour;

@end


//
//  GlobalNormalTime.m
//  YouXianMingClock
//
//  Created by YouXianMing on 14-10-12.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "GlobalNormalTime.h"

static NSDateFormatter* _DMLogDateFormatter = nil;

@implementation GlobalNormalTime

+ (void)initialize
{
    if (self == [GlobalNormalTime class]) {
        // 日期格式
        _DMLogDateFormatter = [[NSDateFormatter alloc] init];
        [_DMLogDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [_DMLogDateFormatter setDateFormat:@"HH:mm:ss"];
    }
}

+ (NSArray *)currentTime
{
    NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
    NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
    return timeArray;
}

+ (float)currentSecond
{
    NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
    NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
    
    // 获取到时间
    float sec =  [timeArray[2] intValue];
    return sec;
}

+ (float)currentMinute
{
    NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
    NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
    
    // 获取到时间
    float min =  [timeArray[1] intValue];
    return min;
}

+ (float)currentHour
{
    NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
    NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
    
    // 获取到时间
    float hour =  [timeArray[0] intValue];
    return hour;
}

@end

以下提供简易分析,使用非常简单,试一下就知道了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值