//
// main.m
// Copyright (c) 2015年 tongxing. All rights reserved.
//
int main(int argc, const char * argv[])
{
@autoreleasepool {
#pragma mark----NSDate的使用
//1.获取当前时间
NSDate *newDate =[NSDate date];
NSLog(@"%@",newDate);
//2.计算从当前时间开始向后多长时间(明天的此时此刻)
NSDate *dataLate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];//默认零时区
NSLog(@"%@",dataLate);
//
NSDate *data1 = [NSDate dateWithTimeIntervalSinceReferenceDate:8*60*60];
NSLog(@"%@",data1);
//3.1970年的参考日期点
NSTimeInterval dataNumber = [newDate timeIntervalSince1970];
NSLog(@"%f",dataNumber);
//4.1990年的参考日期点
NSTimeInterval dataNumber1 = [newDate timeIntervalSinceReferenceDate];
NSLog(@"%f",dataNumber1);//很多时候利用这个方法来获取一个时间戳,当我们在程序中看见类似的10位数字时候,可以联想是否可能是时间戳?
//5.日期之间的相互比较
NSDate *newDate1 = [NSDate date];
NSTimeInterval seconds = [newDate1 timeIntervalSinceDate:newDate];
NSLog(@"%f",seconds);
#pragma mark----NSDateFormatter的使用
//NSDateFormatter这个类是用来处理日期与字符串之间转换的方法。在很多时候,我们需要某个确定的时间点,但是NSDate仅仅提供了如何通过几个特殊时间点,对于比较一般化的时间点,需要我们去计算时间段(TimeInterval),然后再获取我们需要的时间点。这里计算的过程相对较为耗时,这样我们提供了一个可以手写一个字符串,然后将字符串直接转化为日期的类,方便快捷。
//1.将NSdate转变为表示时间的NSString
NSDateFormatter *dataFor = [[NSDateFormatter alloc]init];
[dataFor setDateFormat:@"YYYY年MM月dd日HH时mm分ss秒"];
//把日期转化成字符串形式输出,按照给定的日期格式,必须要通过日期对象来实现
NSDate *date11 = [dataFor dateFromString:@"2015年04月08日10时50分55秒"];//这里把字符串转化为日期对象
NSLog(@"%@",date11);//打印出来的日期是格林尼治时间
NSString *dateStr = [dataFor stringFromDate:date11];
NSLog(@"%@",dateStr);//打印出来自定义的时间格式
//2.表示时间的NSString转换为对应的NSdate对象
NSDateFormatter * formatter1 = [[NSDateFormatter alloc]init];
NSString *str = @"2013-12-10 10:30:00";
[formatter1 setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate * date2 = [formatter1 dateFromString:str];
NSLog(@"%@",date2);
//3.关于NSDateFormate的一些常用的方法:{知识扩展}
NSDate *now= [NSDate date];
NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
fmt.dateStyle = kCFDateFormatterMediumStyle;//日期格式
fmt.timeStyle = kCFDateFormatterMediumStyle;//时间格式
fmt.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];//zh_CN,则为简体中文输出
NSString *dataString =[fmt stringFromDate:now];
NSLog(@"%@",dataString);
//上面的实例中使用的参数是 kCFDateFormatterShortStyle,关于这个参数根据官方API下面给出具体参考:
//@"en_US"
typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles
kCFDateFormatterNoStyle = 0, // 无输出
kCFDateFormatterShortStyle = 1, // 10/29/12, 2:27 PM
kCFDateFormatterMediumStyle = 2, // Oct 29, 2012, 2:36:59 PM
kCFDateFormatterLongStyle = 3, // October 29, 2012, 2:38:46 PM GMT+08:00
kCFDateFormatterFullStyle = 4 // Monday, October 29, 2012, 2:39:56 PM China Standard Time
};
/*
//@"zh_CH"
typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles
kCFDateFormatterNoStyle = 0, // 无输出
kCFDateFormatterShortStyle = 1, // 12-10-29 下午2:52
kCFDateFormatterMediumStyle = 2, // 2012-10-29 下午2:51:43
kCFDateFormatterLongStyle = 3, // 2012年10月29日 GMT+0800下午2时51分08秒
kCFDateFormatterFullStyle = 4 // 2012年10月29日星期一 中国标准时间下午2时46分49秒
};
*/
//4.也可以使用自定义的格式
NSDate* now1 = [NSDate date];
NSDateFormatter* fmt1 = [[NSDateFormatter alloc] init];
fmt1.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
fmt1.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
NSString* dateString1 = [fmt1 stringFromDate:now1];
NSLog(@"%@", dateString1);
}
return 0;
}