Convenience-obtain the system's hour, minute, second, year, month, and day

Source: Internet
Author: User

During project development, we often use time to quickly obtain the current time:

Source code:

Lglobalnormaltime. h and lglobalnormaltime. m

 1 //  Created by Lisa on 14-10-14. 2 //  Copyright (c) 2014年 Lisa. All rights reserved. 3 // 4  5 #import <Foundation/Foundation.h> 6  7 @interface LGlobalNormalTime : NSObject 8  9 10 /*11  当前时间的数组12  @return 返回有3个元素的数组(0处小时、1处位分钟、2处为秒)13  */14 +(NSArray *)currentTime;15 16 /*17  当前秒18  @return 当前秒19  */20 21 +(float)currentSecond;22 23 /*24  当前分钟25  @return 当前分钟26  */27 +(float)currentMinute;28 29 /*30  当前小时31  @return 当前小时32  */33 +(float)currentHours;34 35 36 /*37  当前年份38  @return 当前年份39  */40 +(CGFloat)currentYear;41 42 /*43  当前月份44  @return 当前年份45  */46 +(CGFloat)currentMonth;47 48 /*49  当前day50  @return 当前day51  */52 +(CGFloat)currentDay;53 54 @end
  1 //  Created by Lisa on 14-10-14.  2 //  Copyright (c) 2014年 Lisa. All rights reserved.  3 //  4   5 #import "LGlobalNormalTime.h"  6   7 static NSDateFormatter* _DMLogdateFormatter= nil;  8 static NSDateFormatter* _DMYearDateFormatter= nil;  9  10  11 @implementation LGlobalNormalTime 12  13 +(void)initialize 14 { 15     if (self == [LGlobalNormalTime class]) { 16         //日期格式 17         _DMLogdateFormatter = [[NSDateFormatter alloc]init]; 18         [_DMLogdateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]]; 19         [_DMLogdateFormatter setDateFormat:@"HH:mm:ss"]; 20          21         _DMYearDateFormatter = [[NSDateFormatter alloc]init]; 22         [_DMYearDateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]]; 23         [_DMYearDateFormatter setDateFormat:@"yyyy-MM-dd"]; 24     } 25 } 26  27  28 /* 29  当前时间的数组 30  @return 返回有3个元素的数组(0处小时、1处位分钟、2处为秒) 31  */ 32 +(NSArray *)currentTime 33 { 34     NSString *timeNow = [_DMLogdateFormatter stringFromDate:[NSDate date]]; 35     NSArray *timeArray = [timeNow componentsSeparatedByString:@":"]; 36     NSString *yearNow = [_DMYearDateFormatter stringFromDate:[NSDate date]]; 37     NSArray *yearArray = [yearNow componentsSeparatedByString:@"-"]; 38     NSMutableArray *allArray = [NSMutableArray arrayWithCapacity:1]; 39     [allArray addObjectsFromArray:timeArray]; 40     [allArray addObjectsFromArray:yearArray]; 41      42     return allArray; 43      44 } 45  46 /* 47  当前秒 48  @return 当前秒 49  */ 50  51 +(float)currentSecond 52 { 53     NSArray *timeArray = [self currentTime]; 54     float second  = [timeArray[2] intValue]; 55     return second; 56 } 57  58 /* 59  当前分钟 60  @return 当前分钟 61  */ 62 +(float)currentMinute 63 { 64     NSArray *timeArray = [self currentTime]; 65     float minute  = [timeArray[1] intValue]; 66     return minute; 67 } 68  69 /* 70  当前小时 71  @return 当前小时 72  */ 73 +(float)currentHours 74 { 75     NSArray *timeArray = [self currentTime]; 76     float hour  = [timeArray[0] intValue]; 77     return hour; 78 } 79  80  81  82 /* 83  当前年份 84  @return 当前年份 85  */ 86 +(CGFloat)currentYear 87 { 88     NSArray *timeArray = [self currentTime]; 89     float year  = [timeArray[3] intValue]; 90     return year; 91 } 92 /* 93  当前月份 94  @return 当前年份 95  */ 96 +(CGFloat)currentMonth 97 { 98     NSArray *timeArray = [self currentTime]; 99     float month  = [timeArray[4] intValue];100     return month;101 }102 /*103  当前day104  @return 当前day105  */106 +(CGFloat)currentDay107 {108     NSArray *timeArray = [self currentTime];109     float day  = [timeArray[5] intValue];110     return day;111 }112 113 114 115 @end

Sample Code:

Print the output result:

 

Convenience-obtain the system's hour, minute, second, year, month, and day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.