便利的获取系统的时分秒
源码如下:
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
以下提供简易分析,使用非常简单,试一下就知道了