English 中文(简体)
我怎么把这个日期(或任何日期)定为一个没有时间、分钟和秒钟的国家。
原标题:How do I create the current date (or any date) as an NSDate without hours, minutes and seconds?

我需要在不设时间、分钟和秒钟的情况下,制定目前日期的(或任何NSDate)的,并以尽可能少的行文加以保留?

我需要在日期和时间、分钟和秒上进行一些计算,造成问题,我需要绝对的日期(我希望绝对是正确的短语)。 它用于一个 app。

I m 设定日期:[NSDatedate],增加小时、分钟和秒。 另外,我还增加了几个月时间,因为我有一天有2300个小时,我认为可以节省每天的费用。

因此,理想的是,我需要一个职能,从国家可持续发展局中创造绝对的国家可持续发展。

我很早就问过一个类似问题,但我最后需要的是国家可持续发展局,而不是扼杀,我对具体确定日期格式,例如: y等感到担忧。

最佳回答

首先,你必须理解,即使你从上删除小时、分钟和秒钟,它仍将是一个时刻。 页: 1 这意味着,你必须考虑到时间区。

此处采用一种方法(作为<代码>NSDate的类别加以实施),该方法于同日夜总会NSDate在<代码>自行上填报:

- (NSDate *)midnightUTC {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                   fromDate:self];
    [dateComponents setHour:0];
    [dateComponents setMinute:0];
    [dateComponents setSecond:0];

    NSDate *midnightUTC = [calendar dateFromComponents:dateComponents];
    [calendar release];

    return midnightUTC;
}
问题回答
NSDate *oldDate = [NSDate date];
NSCalendarUnit requiredDateComponents = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit;    
NSDateComponents *components = [[NSCalendar currentCalendar] components:requiredDateComponents fromDate:oldDate]; 
NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:components];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签