• 【代码笔记】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
     
  • 相关阅读:
    linux 和 ubuntu 修改主机名
    Linux删除用户
    ubuntu更新源
    python连接mysql
    用于迭代器的yield return
    Tuple类型
    Action 和 Func
    用iDSDT制作声显卡DSDT
    C#“同步调用”、“异步调用”、“异步回调”
    读懂IL代码就这么简单
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/6707106.html
Copyright © 2020-2023  润新知