如何在今天午夜获取 NSDate 对象? [英] How can I get an NSDate object for today at midnight?
本文介绍了如何在今天午夜获取 NSDate 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取表示当天午夜的 NSDate 对象的最有效方法是什么?
What is the most efficient way to obtain an NSDate object that represents midnight of the current day?
推荐答案
试试这个:
NSDate *const date = NSDate.date;
NSCalendar *const calendar = NSCalendar.currentCalendar;
NSCalendarUnit const preservedComponents = (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay);
NSDateComponents *const components = [calendar components:preservedComponents fromDate:date];
NSDate *const normalizedDate = [calendar dateFromComponents:components];
这篇关于如何在今天午夜获取 NSDate 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文