便利的获取系统的时分秒

简介:

便利的获取系统的时分秒

源码如下:

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

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

目录
相关文章
|
16天前
|
Java
十二时辰与现代时间的互转(精确版)
十二时辰与现代时间的互转(精确版)
26 0
|
3月前
|
JSON 小程序 安全
【经验分享】如何实现小程序日历范围选择功能
【经验分享】如何实现小程序日历范围选择功能
158 7
|
6月前
|
小程序 开发者
微信小程序显示当前系统年月日时分秒
微信小程序显示当前系统年月日时分秒
95 0
|
7月前
|
存储 C语言 C++
软件开发入门教程网之C++ 日期 & 时间
软件开发入门教程网之C++ 日期 & 时间
|
10月前
|
消息中间件 Dubbo NoSQL
老板,JDK8的日期、时间函数我不熟悉?
介绍JDK 8中的新日期工具类,及整理成PDF文档
53 0
|
11月前
计算LocalDate之间的天数差,方便快捷
计算LocalDate之间的天数差,方便快捷
228 0
|
11月前
|
存储 Java 程序员
实战:求年月日时间前后遇到的坑和解决方式
这周接到一个时间转换任务需要处理,本来没什么问题,后来完成后发现时间有偏差,又重写了一遍代码,感觉很有记录必要性,希望看过的小伙伴可以避坑。
实战:求年月日时间前后遇到的坑和解决方式
|
安全 Java Linux
正确认识及掌握时间的用法
时间是一个相对地区而言的概念,因此有一个基准地区,就是本初子午线穿过的地区。了解世界时间相关的概念可以更好地协调全球人们的活动,便于跨越不同地区的时差。比如按照UTC时区划分算,洛杉矶和北京 之间的时间差异是16个小时, 但是一旦洛杉矶启用了夏令时两者之间的时间差异只有15个小时,神奇吗?
184 0
正确认识及掌握时间的用法
|
存储 算法 Unix
C++ 日期和时间编程总结
在 C++11 之前,C++ 编程只能使用 C-style 日期时间库,其精度只有秒级别,这对于有高精度要求的程序来说,是不够的。但这个问题在C++11 中得到了解决,C++11 中不仅扩展了对于精度的要求,也为不同系统的时间要求提供了支持。另一方面,对于只能使用 C-style 日期时间库的程序来说,C++17 中也增加了 timespec 将精度提升到了纳秒级别。
315 1
|
定位技术 C#
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换