首页 > 其他 > 详细

便利的获取系统的时分秒

时间:2014-10-12 20:04:59      阅读:205      评论:0      收藏:0      [点我收藏+]

便利的获取系统的时分秒

bubuko.com,布布扣

源码如下:

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

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

bubuko.com,布布扣

bubuko.com,布布扣

 

便利的获取系统的时分秒

原文:http://www.cnblogs.com/YouXianMing/p/4020954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!