NSDate的使用

NSDate是OC中的时间类型,和Java中的Date一样,而对时间进行格式化处理的NSDateFormatter就像Java中simpledateformat,对时间进行格式化输出或者对一个字符串时间转换成时间格式。

基本使用

NSDate *date=[NSDatedate];

       NSLog(@"date=%@",date);

        //1970-1-1 0 0 0100

        [NSDatedateWithTimeIntervalSince1970:100];

        //比当前时间晚100

        [NSDatedateWithTimeIntervalSinceNow:100];

        //随机返回一个将来的时间

        [NSDatedistantFuture];

        //随机访问一个过去的时间

        [NSDatedistantPast];

        

       NSDate *date2=[NSDatedate];

        

        //返回早一点的时间

       NSLog(@"%@",[dateearlierDate:date2]);

        //返回晚一点的时间

       NSLog(@"%@",[datelaterDate:date2]);

        

        //返回从1970到现在的秒数

        NSLog(@"%zd",[datetimeIntervalSince1970]);

格式化使用

//格式化 HH24小时 hh12小时

        NSDateFormatter *format=[[[NSDateFormatteralloc]init]autorelease];

        format.dateFormat=@"yyyy-MM-dd hh:mm:ss";

        //时间转字符串输出

       NSLog(@"format=%@",[formatstringFromDate:date]);

        

        NSString *times=@"2014-02-02 12:10:25";

        //字符串转时间

       NSLog(@"%@",[formatdateFromString:times]);


NSDate的使用

上一篇:adb.exe问题


下一篇:MySql引擎