main.h
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#pragma mark NSDate:獲取時(shí)間的類
/**
* NSDate是iOS中表示日期、時(shí)間的數(shù)據(jù)類型垫桂,包含年月日時(shí)分秒,提供了日期的創(chuàng)建妇穴。比較洒扎、時(shí)間間隔計(jì)算的功能
*
**/
// // 獲取當(dāng)前時(shí)間:獲取到的date對(duì)象是零時(shí)區(qū)的時(shí)間
NSDate *nowDate = [NSDate date];
NSLog(@"%@",nowDate);
// 獲取當(dāng)前時(shí)區(qū)的時(shí)間
NSString *localDate = [nowDate descriptionWithLocale:[NSLocale currentLocale]];
NSLog(@"%@",localDate);
// 獲取距離當(dāng)前時(shí)間若干秒之后的時(shí)間:NSTimeInterval是一個(gè)以秒為單位的時(shí)間間隔获高,是一個(gè)double類型的數(shù)據(jù)
NSDate *eightAreaDate = [NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSLog(@"=====%@",eightAreaDate);
// 獲取明天這個(gè)時(shí)間的時(shí)間
NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:32*60*60];
NSLog(@"tomorrowDate = %@",tomorrowDate);
// 獲取時(shí)間戳
// 什么是時(shí)間戳驯杜?從1970年1月1日開始到當(dāng)前時(shí)間經(jīng)過(guò)了多少秒
NSTimeInterval timeInterval = [nowDate timeIntervalSince1970];
NSLog(@"%f",timeInterval);
// 將時(shí)間戳轉(zhuǎn)換為當(dāng)前日期
NSDate *InterValDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSLog(@"%@",InterValDate);
// 獲取兩個(gè)時(shí)間之間的間隔
NSTimeInterval betweenTwoTimes = [tomorrowDate timeIntervalSinceDate:eightAreaDate];
NSLog(@"%f",betweenTwoTimes);
// NSDate *nowTime = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60];
// NSDate *Time1 = [NSDate dateWithTimeIntervalSinceNow:10 * 60 * 60];
//// NSDate *Time2 = [NSDate dateWithTimeIntervalSinceNow:9*60*60];
//
//// 獲取時(shí)間差值 :用第一個(gè)對(duì)象減去第二個(gè)對(duì)象膜宋,得到他們的差值
// NSTimeInterval time = [Time1 timeIntervalSinceDate:nowTime];
// NSLog(@"===%f",time);
// // 判斷差值
// if (time < 60) {
// NSLog(@"剛剛");
// }else if(time > 60 && time < 3600){
// NSLog(@"%d分鐘前",(int)time/60);
// }else if(time > 3600 ){
// NSLog(@"%d時(shí)前",(int)time/60/60);
// }
#pragma mark NSDateFormatter 時(shí)間的格式轉(zhuǎn)換類
/**
* NSDateFormatter 是iOS中的日期格式類窿侈,主要實(shí)現(xiàn);將代表日期的NSDate對(duì)象轉(zhuǎn)換為字符串對(duì)象秋茫,或?qū)r(shí)間字符串轉(zhuǎn)換為日期對(duì)象
* NSDateFormatter 可以根據(jù)我們提供的日期格式史简,將NSDate轉(zhuǎn)換為指定格式的字符串
* 我們有兩種方法指定日期格式:1.使用系統(tǒng)預(yù)置的日期格式。2.自定義日期格式
* 在將時(shí)間對(duì)象轉(zhuǎn)換為字符串對(duì)象時(shí)肛著,會(huì)先自動(dòng)將時(shí)間對(duì)象轉(zhuǎn)變?yōu)楫?dāng)前時(shí)區(qū)的時(shí)間圆兵,再轉(zhuǎn)換為字符串
* 在將字符串格式的時(shí)間轉(zhuǎn)換為時(shí)間對(duì)象時(shí),現(xiàn)將本失去時(shí)間轉(zhuǎn)換為零時(shí)區(qū)時(shí)間枢贿,然后再轉(zhuǎn)換為事件對(duì)象
**/
// 使用系統(tǒng)預(yù)置的日期格式
// 1.創(chuàng)建一個(gè)NSDateFormatter對(duì)象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 2.設(shè)置日期格式
[formatter setDateStyle:NSDateFormatterFullStyle];
// 3.設(shè)置時(shí)間格式
[formatter setTimeStyle:NSDateFormatterFullStyle];
// 4.創(chuàng)建date對(duì)象殉农,進(jìn)行轉(zhuǎn)換
NSString *str = [formatter stringFromDate:nowDate];
NSLog(@"%@",str);
// 使用自定義方式定義時(shí)間格式
// 1.創(chuàng)建NSDateFormatter對(duì)象
NSDateFormatter *selfFormatter = [[NSDateFormatter alloc] init];
[selfFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SS"];
NSString *str1 = [selfFormatter stringFromDate:nowDate];
NSLog(@"%@",str1);
// 將一個(gè)字符串格式的時(shí)間轉(zhuǎn)換為時(shí)間對(duì)象
// 創(chuàng)建出字符串格式的時(shí)間
NSString *dateString1 = @"2015年11月03號(hào) 14時(shí)52分00秒";
// 1. 創(chuàng)建出NSDateFormatter對(duì)象
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
// 2. 設(shè)置對(duì)應(yīng)的時(shí)間格式,該格式要嚴(yán)格和字符串時(shí)間匹配局荚,否則不能正常轉(zhuǎn)換
[formatter1 setDateFormat:@"yyyy年MM月dd號(hào) HH時(shí)mm分ss秒"];
// 3. 調(diào)用formatter對(duì)象统抬,將字符串格式的時(shí)間轉(zhuǎn)換為時(shí)間對(duì)象
NSDate *dataFormString = [formatter1 dateFromString:dateString1];
NSLog(@"%@",dataFormString);
return 0;
}