Foundation架構,foundation
NSDate類
//// main.m// 8.NSDate//// Created by wangzhaolu on 14-2-25.// Copyright (c) 2014年 Turing. All rights reserved.//#import <Foundation/Foundation.h>static void transTimeZone(NSString* currDateStr);int main(int argc, const char * argv[]) { @autoreleasepool {
NSTimeInterval 時間間隔
//擷取當前日期的兩種方式: //NSDate* date =[[NSDate alloc]init]; NSLog(@"******************** NSTimeInterval **********************"); //@property(readonly) NSTimeInterval timeIntervalSinceNow NSDate* date = [NSDate date]; NSLog(@"目前時間為:%@",date); //目前時間到現在的時間間隔 (理論值為0) NSTimeInterval dateIntvl=[date timeIntervalSinceNow]; NSLog(@"目前時間到現在的時間為:%f",dateIntvl); //從1970年到現在的時間間隔 dateIntvl=[date timeIntervalSince1970]; NSLog(@"從1970年到現在的時間為:%f",dateIntvl); //從蘋果的參考時間到現在的時間間隔 dateIntvl=[date timeIntervalSinceReferenceDate]; NSLog(@"從蘋果參考時間到現在的時間為:%fu",dateIntvl); //從現在開始的2天后的時間 NSTimeInterval nextIntvl=2*24*60*60; NSDate* next=[NSDate dateWithTimeIntervalSinceNow:nextIntvl]; NSLog(@"從現在開始2天后的時間為:%@",next);
NSDateFormatter 日期格式化
NSLog(@"*********** NSDateFormatter 日期格式化*********************"); NSDate* curDate=[NSDate new]; NSDateFormatter* fmt=[[NSDateFormatter alloc]init]; //dateStyle 系統日期格式 fmt.dateStyle=NSDateFormatterShortStyle; //14-12-26 fmt.dateStyle=NSDateFormatterLongStyle; //2014年12月26日 fmt.dateStyle=NSDateFormatterFullStyle; //2014年12月26日 星期五 NSString* fmtStr=[fmt stringFromDate:curDate]; NSLog(@"格式化後的日期為:%@",fmtStr); NSDateFormatter* fmt1=[[NSDateFormatter alloc]init]; //dateFormat 自訂日期格式 fmt1.dateFormat=@"yyyy/MM/dd HH:mm"; //2014/12/26 11:57 fmt1.dateFormat=@"yyyy-MM-dd hh:mm:ss"; //2014-12-26 11:57:30 fmt1.dateFormat=@"yyyy年MM月dd日 hh時mm分ss秒SSSSS"; //2014y12m26日 12時01分52秒15700 fmt1.dateFormat=@"yy.MM.dd hh.mm.ss"; NSString* fmtStr1=[fmt1 stringFromDate:curDate]; NSLog(@"格式化後的日期為:%@",fmtStr1); //將字串格式轉換為日期格式 NSString* source=@"1980-12-15"; NSDateFormatter* df=[NSDateFormatter new]; NSString* dateFmt=@"yyyy-MM-dd"; df.dateFormat=dateFmt; df.timeZone =[NSTimeZone timeZoneWithName:@"Europe/London"]; NSDate* dt=[df dateFromString:source]; //輸出時間戳記 NSTimeInterval intvl=[dt timeIntervalSinceReferenceDate]; NSLog(@"轉換後的時間為:%@,時間戳記為:%f",dt,intvl);
NSTimeZone 時區轉換
NSLog(@"****************** NSTimeZone 時區轉換********************"); //遍曆時區對象NSTimeZone NSArray* zoneNames=[NSTimeZone knownTimeZoneNames]; for (int i=0; i<zoneNames.count; i++) { NSLog(@"%@",zoneNames[i]); } //調用時區轉換函式 transTimeZone(@"2008-8-8 20:08:08");
//時區轉換static void transTimeZone(NSString* currDateStr){ NSDate* currDate=nil; NSLog(@"北京時間:%@",currDateStr); NSDateFormatter *fmt3=[NSDateFormatter new]; fmt3.dateFormat=@"yyyy-MM-dd HH:mm:ss"; currDate =[fmt3 dateFromString:currDateStr]; //北京時間轉為東京時間 fmt3.timeZone=[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]; NSString* zoneTransed=[fmt3 stringFromDate:currDate]; NSLog(@"東京時間:%@",zoneTransed); //北京時間轉為紐約時間ff fmt3.timeZone=[NSTimeZone timeZoneWithName:@"Europe/Moscow"]; NSString* zoneTransed1=[fmt3 stringFromDate:currDate]; NSLog(@"墨西哥時間:%@",zoneTransed1); //北京時間轉換為紐約時間 fmt3.timeZone=[NSTimeZone timeZoneWithName:@"America/New_York"]; NSString* zoneTransed2=[fmt3 stringFromDate:currDate]; NSLog(@"紐約時間:%@",zoneTransed2);}
NSCalendar 日曆與日期
NSLog(@"****************** NSCalendar 日曆與日期*******************"); NSString* source1=@"2014-2-14"; NSDateFormatter* df2=[NSDateFormatter new]; df2.dateFormat=@"yyyy-MM-dd"; //求當月最後一天是幾號?(求這個月有多少天) NSCalendar* cal=[NSCalendar currentCalendar]; NSDate* date1 =[df2 dateFromString:source1]; // smaller =天(NSCalendarUnitDay) // larger =月 (NSCalendarUnitMonth) NSRange rng=[cal rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date1]; NSLog(@"%@這個月共有%ld天",source1,rng.length); //設定 每周的第一天 1=sunday 2=monday [cal setFirstWeekday:2]; //求本周是星期幾? NSUInteger day=[cal ordinalityOfUnit:NSCalendarUnitDay inUnit:NSWeekCalendarUnit forDate:date1]; NSLog(@"今天是星期:%lu",day);
NSDateComponents 獲得日期的各項值
NSLog(@"*********** NSDateComponents 獲得日期的各項值 *************"); //通過NSDateComponents獲得日期的各項值 (| "或") NSCalendarUnit units=NSCalendarUnitDay|NSCalendarUnitMonth |NSCalendarUnitWeekday|NSCalendarUnitYear; NSDateComponents* ncp=[cal components:units fromDate:date1]; NSLog(@"輸出ncp:%@",ncp.description); //求下個月的這一天是星期幾? ncp.month +=1; NSDate* newDate =[cal dateFromComponents:ncp]; NSUInteger newday=[cal ordinalityOfUnit:NSCalendarUnitDay inUnit:NSWeekCalendarUnit forDate:newDate]; NSLog(@"下個月的今天[%@]是星期:%lu",[df stringFromDate:newDate],newday); }