iOS擷取當天0點

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   com   

  最近一個公司一個項目裡有個坑,顯示不了0點的資料,修改起始時間為前一天23點59分59秒則可以顯示。

  查看了擷取當天0點代碼,和網上查到的結果基本一致,如下所示(使用Category On NSDate):

1 - (NSDate *)zeroOfDate2 {3     NSCalendar *calendar = [NSCalendar currentCalendar];4     NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self];5     components.hour = 0;6     components.minute = 0;7     components.second = 0;8     return [calendar dateFromComponents:components];9 }

  加上Log發現return的date NSTimeInterval的小數點後的值不為0,因為nanosecond不為0,而iOS裡又不能使用components.nanosecond = 0,於是用以下辦法解決:

 1 - (NSDate *)zeroOfDate 2 { 3     NSCalendar *calendar = [NSCalendar currentCalendar]; 4     NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:self]; 5     components.hour = 0; 6     components.minute = 0; 7     components.second = 0; 8      9     // components.nanosecond = 0 not available in iOS10     NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];11     return [NSDate dateWithTimeIntervalSince1970:ts];12 }

  做個標記,以防以後被坑。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.