zoukankan      html  css  js  c++  java
  • 【代码笔记】iOS-获取现在的日历时间

    一,代码。

    复制代码
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        NSLog(@"-------getCurrentTime---%@",[self getCurrentTime]);
        
    }
    
    #pragma -mark -获取现在的日历时间  输出形式 2014-06-27 15:41:55
    - (NSString *) getCurrentTime
    {
        
        NSDate* date = [NSDate date];
        
        //日历为阳历
         NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        //表示一个日期对象的组件
        NSDateComponents *component = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
        //设定时区
        [component setTimeZone:[NSTimeZone systemTimeZone]];
        
        NSInteger year = [component year];
        NSInteger month = [component month];
        NSInteger day = [component day];
        NSInteger hour = [component hour];
        NSInteger minute = [component minute];
        NSInteger second = [component second];
        
        NSString* dateString = [NSString stringWithFormat:@"%i-%@%i-%@%i %@%2i:%@%2i:%@%i" , year , month > 10 ? @"" :@"0",month , day > 10 ? @"" :@"0", day , hour > 10 ? @"" :@"0", hour  , minute > 10 ? @"" :@"0", minute ,second > 10 ? @"" :@"0", second];
        return dateString;
        
    }
    复制代码

    二,输出。

    2015-10-22 14:06:16.920 getCurrentTime[11139:215609] -------getCurrentTime---2015-010-22 14:0 6:16
     
  • 相关阅读:
    mysql表的查询(连接查询)练习
    mysql基础语法
    Linux 常用命令整理
    1.django 环境搭建
    2.django 操作笔记
    mysql基础笔记(1)
    VMware复制Linux虚拟机后网络配置
    uC/OS-III 软件定时器(三)
    uC/OS-III 时间管理(二)
    uC/OS-III 时钟节拍(一)
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/6707106.html
Copyright © 2011-2022 走看看