NSDate及NSDaterFormatter

来源:互联网 发布:淘宝助理和千牛 编辑:程序博客网 时间:2024/06/06 07:13
////  main.m//  8_9_类的扩展及NSDate////  Created by lanou3g on 15/8/9.//  Copyright (c) 2015年 lanou3g. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {#pragma mark ---------------NSDate-----        //创建日期:表示当前的时间        //NSData是数据类        //涉及到时区的问题,全球一共24个时区,北京 东八区        //NSDate输出的是0时区的时间,0时区又叫格林尼治时间GMT        NSDate *now = [NSDate date];        NSLog(@"%@",now);        //创建日期以当前时间为参考点,创建表示明天这个时间的日期对象,用下面的方法,后面的就是加上的时间。昨天的时间用负号。        NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:24*3600];        NSLog(@"%@",date1);        //dateWithTimeTntervalReferenceDate是以2001.1.1零时为参考点的        NSDate *date2 = [NSDate dateWithTimeIntervalSinceReferenceDate:3600];        NSLog(@"%@",date2);        //dateWithTimeIntervalSince1970是以1970.1.1零时零分零秒年为参考点的        NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:3600];        NSLog(@"%@",date3);        //dateWithTimeIntarvarl是以自己指定的时间为参考点        NSDate *date4 = [NSDate dateWithTimeInterval:3600 sinceDate:date3];        NSLog(@"%@",date4);        double n = [date3 timeIntervalSinceDate:date4];        NSLog(@"%f",n);        n = [[NSDate date] timeIntervalSince1970];        NSLog(@"%f",n);#pragma mark--------------NSDateFormatter 日期格式化类        //将NSDate 转换为NSStirng        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];        //设定日期转换的格式,  年y 月M 日d 小时 h  分 m 秒 s 时区z  星期e      如果格式化以后显示的就是当前时区的        //字母的个数不同,显现的内容不懂        [dateFormatter setDateFormat:@"yyyy.MMMM.dd  hhhh:mm:ss:eeeee  zzzz"];        //开始转换        NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];        NSLog(@"%@",dateString);        //将NSString 转换为NSDate        //需要设置转换格式,格式要匹配        NSString *timeStr = @"2014年05月01日 10点23分18秒";        [dateFormatter setDateFormat:@"yy年M月dd日 h点mm分s秒"];        NSDate *date6 = [dateFormatter dateFromString:timeStr];        NSLog(@"%@",date6);    }    return 0;}
0 0