在專案中日期的顯示經常會當天的顯示時分,當月的顯示日時和分,以此類推,難免會涉及到日期的比較,下面介紹一下日期比較的兩種方法
比較日期有兩種方法
一種是通過系統的nscalendar類實現
nsstring * date = @"2016-10-12 13:12:12";
//建立日期格式
nsdateformatter * dateformat = [[nsdateformatter alloc]init];
[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];
[dateformat setlocale:[[nslocale alloc] initwithlocaleidentifier:@"en_us"]];
//字串轉為日期
nsdate *showdate =[dateformat datefromstring:date];
//建立日曆類
nscalendar * calendar = [nscalendar currentcalendar];
//比較現在的時間和
nsdatecomponents * components = [calendar components:nscalendarunityear|nscalendarunitmonth|nscalendarunitday fromdate:showdate todate:[nsdate date] options:nscalendarwrapcomponents];
if (components.year) elseelse{
nslog(@"不同月");
另一種方法是:
利用時間的例項方法timeintervalsincedate:就會得出兩個時間相差的秒數,再計算相差的天數
nsstring * date = @"2016-10-13 9:04:00";
//建立日期格式
nsdateformatter * dateformat =[[nsdateformatter alloc]init];
[dateformat setdateformat:
@"yyyy-mm-dd hh:mm:ss"];
[dateformat setlocale:[[nslocale alloc] initwithlocaleidentifier:
@"en_us
"]];
//字串轉為日期
nsdate *showdate =[dateformat datefromstring:date];
nsdate * nowdate =[nsdate date];
nstimeinterval timeinterval =[nowdate timeintervalsincedate:showdate];
nslog(
@"分差=%f
",timeinterval/60.00);//
分差 nslog(@"
時差=%f
",timeinterval/3600.00);//
時差 nslog(@"
天數差=%f
",timeinterval/3600.00/24);//
天數差,如果是0說明是當天,否則不是當天
利用CAsyncSocket類實現Socket程式設計
隨著計算機網路化的深入,計算機網路程式設計在程式設計的過程中變得日益重要。許多文章都曾經介紹過用vc 進行socket程式設計的方法。但由於都是直接利用動態連線庫wsock32.dll進行操作,實現比較繁瑣。其實,vc 的mfc類庫中提供了casyncsocket這樣乙個套接字類,用它來實現sock...
利用模板類實現功能
templategenericarray genericarray int length size length count 0 使用模板定義替換genericarray宣告,使用模板成員函式替換genericarray的成員函式。定義乙個空陣列,初始化陣列長度。templatevoid gener...
利用MFC的CClientDC類實現畫線功能
cclientdc 派生於cdc 類,在建構函式時呼叫 getdc 函式,在析構時呼叫 releasedc 函式。cclientdc dc this cclientdc dc getparent 可繪製直線到工具欄等地方 dc.moveto m ptorigin dc.lineto point mf...