便利的获取系统的时分秒

简介:

便利的获取系统的时分秒

源码如下:

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

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

目录
相关文章
|
5天前
|
前端开发 安全 Unix
Python编程手册系列 - 日历、日期、时间相关内建模块详解
Python编程手册系列 - 日历、日期、时间相关内建模块详解
72 0
|
6月前
|
小程序 开发者
微信小程序显示当前系统年月日时分秒
微信小程序显示当前系统年月日时分秒
99 0
|
7月前
|
存储 C语言 C++
软件开发入门教程网之C++ 日期 & 时间
软件开发入门教程网之C++ 日期 & 时间
|
10月前
|
消息中间件 Dubbo NoSQL
老板,JDK8的日期、时间函数我不熟悉?
介绍JDK 8中的新日期工具类,及整理成PDF文档
55 0
|
11月前
计算LocalDate之间的天数差,方便快捷
计算LocalDate之间的天数差,方便快捷
230 0
|
11月前
|
存储 Java 程序员
实战:求年月日时间前后遇到的坑和解决方式
这周接到一个时间转换任务需要处理,本来没什么问题,后来完成后发现时间有偏差,又重写了一遍代码,感觉很有记录必要性,希望看过的小伙伴可以避坑。
实战:求年月日时间前后遇到的坑和解决方式
|
存储 算法 Unix
C++ 日期和时间编程总结
在 C++11 之前,C++ 编程只能使用 C-style 日期时间库,其精度只有秒级别,这对于有高精度要求的程序来说,是不够的。但这个问题在C++11 中得到了解决,C++11 中不仅扩展了对于精度的要求,也为不同系统的时间要求提供了支持。另一方面,对于只能使用 C-style 日期时间库的程序来说,C++17 中也增加了 timespec 将精度提升到了纳秒级别。
319 1
|
Python
Python 技术篇-按任意格式灵活获取日期、时间、年月日、时分秒。日期格式化。
Python 技术篇-按任意格式灵活获取日期、时间、年月日、时分秒。日期格式化。
634 0