zoukankan      html  css  js  c++  java
  • NSDate

    //    NSDate日期类
        NSDate *date = [NSDate date];
        NSLog(@"%@", date);//0时区
            //打印出来的格式 -- :: 时区
        
    //    NSTimeInterval:时间间隔类,实质是double类型,单位是秒
        NSTimeInterval timeInterval = 60;
        NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:60];
        NSLog(@"%@", date1);
            //昨天的现在时刻
        
        NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
        NSLog(@"%@", date2);
            //明年的现在时刻
        NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:365*24*60*60];
        NSLog(@"%@", date3);
        
            //昨天的现在时刻  明年的现在时刻 的时间间隔是多少秒
        NSTimeInterval timeInterval2 = [date2 timeIntervalSinceDate:date3];
        NSLog(@"%lf", timeInterval2);
        
            //现在的时刻 197011日的时间间隔是多少秒
        NSTimeInterval timeInterval3 = [date timeIntervalSince1970];
         NSLog(@"%lf", timeInterval3);
        
            //时间戳:某一时刻与197011日的时间间隔
            //例如:服务器传过来的时间戳为1416882400
        
        NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:1416882400];
        NSLog(@"%@", date4);
        
        NSDate *nowDate = [NSDate date];

    //    NSDateFormatter , 日期格式类,

    //yyyy:四位年份
    //yy:后两位为年份

    //MM:2位月份 :后两位为年份
    //MM:位月份
    2位月份

         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                        //设置日期格式
        [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];


            //设置时区
        NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8 * 60 * 60];
        [formatter setTimeZone:timeZone];

        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"china"];//时区名字或地区名字
        [formatter setTimeZone:timeZone];
        
            //按照formatter规定的格式,把日期转成字符串
        NSString *timeString = [formatter stringFromDate:nowDate];
        NSLog(@"%@", timeString);
        
        
        将字符串"20140501 102318"  转换为日期格式
        NSString *time = @"20140501 102318";
        NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
        [formatter1 setDateFormat:@"yyyyMMdd hhmmss"];
        
        
        [formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GTM"]];//GTM,格林尼治时区
        NSDate *date = [formatter1 dateFromString:time];
        NSLog(@"%@", date);

                                                         

    [[NSDateFormatter alloc] init]; NSDateFormatter alloc] init];[[NSDateFormatter alloc] init]

     
  • 相关阅读:
    [ZJOI2007]时态同步 题解
    Xposed 在android 6.0上报couldn't load class,找不到xposed_init中配置的入口类
    微信小程序http 400问题
    在Mac上 python中使用tesseract OCR (Pytesser) 识别图片中的文字
    微信小游戏跳一跳简单手动外挂(基于adb 和 python)
    第一个微信小程序踩的几个小坑
    android studio/Intellij IDEA(MAC OSX)中android模拟器无法启动的一种原因
    【转载】word2vec原理推导与代码分析
    HTTP Get Post究竟有哪些区别
    初试kotlin:用Kotlin开发桌面/CommandLine 工具
  • 原文地址:https://www.cnblogs.com/tian-sun/p/4309574.html
Copyright © 2011-2022 走看看