iOS階段學習第15天筆記 NSDate操作

2022-03-07 11:50:11 字數 4817 閱讀 2413

ios學習(oc語言)知識點整理

一、oc關於nsdate類的操作例項**

1

//預設直接顯示物件,顯示的是格林威治時間 2//

獲取當前日期時間的例項方法

3 nsdate *date1=[[nsdate alloc]init];

4 nslog(@"

date1:%@

",date1); 5

8//獲取當前日期時間的方法二:類方法

9 nsdate *date2=[nsdate date];

10 nslog(@"

date2:%@

",date2);

1112

//獲取從2023年1月1日00:00到date2的秒數

13 nstimeinterval interval=[date2 timeintervalsince1970];

12//

迴圈操作為了體現時間差

13for(int i=0;i<10000;i++){}

14//

計算兩個時間差(秒數)

15 interval=[date2 timeintervalsincedate:date1];

1617

//date2距現在的時間差

18 interval=[date1 timeintervalsincenow];

19 nslog(@"%f"

,interval);

2021

//例項方法:得到date2的後一天的時間物件

22 nsdate *date3=[date2 datebyaddingtimeinterval:24*3600

];

23 nslog(@"

date3:%@

",date3);

2425

//類方法:得到距date2多少秒後的乙個日期時間物件

26 nsdate *date4=[nsdate datewithtimeinterval:24*3600

sincedate:date2];

27 nslog(@"

date4:%@

",date4);

2829

//得到距現在多少秒後的乙個日期時間物件

30 nsdate *date5=[nsdate datewithtimeintervalsincenow:3*24*3600

];

31 nslog(@"

date5:%@

",date5);

3233

//獲取將來的乙個日期時間物件

34 nsdate *date6=[nsdate distantfuture];

35 nslog(@"

date6:%@

",date6);

3637

//獲取過去的乙個日期時間物件

38 nsdate *date7=[nsdate distantpast];

39 nslog(@"

date7:%@

",date7);

4041

//對日期時間物件做格式化操作的類

42 nsdateformatter *format=[[nsdateformatter alloc]init];

43//

設定日期的樣式

44[format setdatestyle:nsdateformattermediumstyle];

45//

設定時間的樣式

46[format settimestyle:nsdateformattermediumstyle];

47 nsstring *styledate=[format stringfromdate:date1];

48 nslog(@"

styledate:%@

",styledate);

47//

自定義格式字串

48 [format setdateformat:@"

yyyy-mm-dd hh:mm:ss

"];

49//

將nsdate物件轉換為字串

50 nsstring *sdate=[format stringfromdate:date1];

51 nslog(@"

sdate:%@

",sdate);

5253 nsstring *str=@"

2015-07-10 12:10:30

";

54//

將字串形式的日期時間轉換為nsdate物件(保證字串的形式與format中的形式一致)

55 nsdate *date8=[format datefromstring:str];

56 nslog(@"

date8:%@

",date8);

5758 nsdate *newdate=[nsdate date];

59//

時間格式化字串

60 nsdateformatter *formatter =[[nsdateformatter alloc]init];

61 [formatter setdateformat:@"

yyyy-mm-dd hh:mm:ss mmm eee a"];

62 nsstring *strdate =[formatter stringfromdate:newdate];

63 nslog(@"%@"

,strdate);

6465

//時間字串轉換為時間型別

66 nsdateformatter * formatter2 =[[nsdateformatter alloc]init];

67 nsstring * nstr =@"

2023年05月01日 10點23分18秒";

68 [formatter2 setdateformat:@"

yyyy年mm月dd日 hh點mm分ss秒"];

69 nsdate * date3 =[formatter2 datefromstring:nstr];

70//

解決時分秒轉換不準確問題

71 nstimezone *fzone =[nstimezone systemtimezone];

72 nsinteger finterval =[fzone secondsfromgmtfordate: date3];

73 nsdate *fdate =[date3 datebyaddingtimeinterval: finterval];

74 nslog(@"%@"

,fdate);

7576

//獲取系統時間的年月日時分秒部分

77 nscalendar *cal =[nscalendar currentcalendar];

78//

mac 10.10以前的系統使用 nsyearcalendarunit

79 unsigned int unitflags = nscalendarunityear |nscalendarunitmonth|nscalendarunitday|nscalendarunithour|nscalendarunitminute|nscalendarunitsecond;

80 nsdatecomponents *d =[cal components:unitflags fromdate:newdate];

81 nslog(@"

年=%ld

",[d year]);

82 nslog(@"

月=%ld

",[d month]);

83 nslog(@"

日=%ld

",[d day]);

84 nslog(@"

時=%ld

",[d hour]);

85 nslog(@"

分=%ld

",[d minute]);

86 nslog(@"

秒=%ld

",[d second]);

8788

//將傳入時間字串轉換成需要的時間格式

89 nsstring *datestr=@"

2013-08-13 20:28:40";

90 nsdateformatter *format=[[nsdateformatter alloc] init];

91 [format setdateformat:@"

yyyy-mm-dd hh:mm:ss"];

92 nsdate *fromdate=[format datefromstring:datestr];

93//

解決時分秒轉換不準確問題

94 nstimezone *fromzone =[nstimezone systemtimezone];

95 nsinteger frominterval =[fromzone secondsfromgmtfordate: fromdate];

96 nsdate *fromdate =[fromdate datebyaddingtimeinterval: frominterval];

97 nslog(@"

fromdate=%@

",fromdate);

98

Python學習第15天 模組

1 模組 每個.py都可以稱為 模組 都被看做是乙個獨立的檔案。在 模組 中,我們可以存放變數 函式 類等 2 模組的作用 模組 中,可以有多個函式 多個變數 多個類,功能更加豐富 1 import import 匯入整個模組,可以使用as取別名 2 from import from import ...

iOS 階段學習第十天筆記(結構體)

ios學習 c語言 知識點整理 一 資料結構 1 概念 資料結構是指電腦程式中所操作的物件 資料以及資料元素之間的相互關係和運算。2 結構體必須有struct 關鍵字修飾.例項 1 struct person 67 intmain struct person 是資料結構9 tom 是變數名 10 t...

學習iOS開發的第16天

今天主要是做專案,所有沒有花多少時間學習新知識。主要看了下訪問相簿的功能。新建乙個專案,然後在專案裡新建乙個檢視控制器mainviewcontroller。在控制器檢視中新增乙個檢視和乙個按鈕。當按下按鈕時,會執行chooseimage方法。void loadview在chooseimage方法中,...