English 中文(简体)
在核心数据中仅添加日期作为NSDate
原标题:Adding only date into core data as NSDate

我有一个网络服务,可以收回一名JSON物体。 答复包括UNIX(milliseconds)格式的复读时间。 我将UNIX时间邮票改为NSDate,其代码如下:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[timeInUNIX objectAtIndex:i]doubleValue]/1000];

这使我有时间以形式发言。

2009-11-13 19:47:49 +0000

我只想到这个日期。 这一点很重要,因为这一天用于国家安全局的描述人员,也作为表层的主人,因此,我把日期和时间与以下法典分开:

+ (NSDate *)dateWithOutTime:(NSDate *)datDate {
    if( datDate == nil ) {
        datDate = [NSDate date];
    }
    NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
    return [[NSCalendar currentCalendar] dateFromComponents:comps];
}

The problem here is the returned NSDate object still contains a junk time value. Is there any way to store only the date in NSDate format?

Can someone help me with this?

Regards, iSee

最佳回答

NSDate必须有一个时间——这是自参考日期以来的几秒钟,它代表着一个具体的时间点。

If you are only interested in the day/month/year sections, the correct way to deal with this is to use a date formatter or date components when presenting the date, rather then when you store it. Alternatively, store the date as a string, but this way you lose any date functionality.

问题回答

两种可能的解决办法,两者都不是非常粗略的:

  1. Take the NSTimeInterval from the date, then truncate it down to the nearest 60*60*24 (seconds*minutes*hours). Something like interval = floor(interval - interval % (60*60*24)). Create a new date from that.
  2. Convert the date into a string showing only the date, then convert that string back to NSDate (might still have junk time components, though, but you can pad the string with 0 values).




相关问题
How do you create UIBarButtonItems with a radio interface?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up ...

iPhone settings bundle

I want to allow the user to enter a valid date using the iPhone’s settings application. I have experimented with many of the PreferenceSpecifiers data node types including date. I have two issues: ...

Circular #import/@class problem in ObjectiveC

I m going to use an example to properly illustrate my confusion. I can t quite wrap my head around this. In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC ...

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 ...

Cocoa-Touch: issue looping MPMoviePlayerController

I have an app which has to load some data at startup, so I want to display a splash-screen animation. I m using the MPMoviePlayerController to play a m4v file. The movie has it s background set to [...

Iphone sequential animation with setAnimationDelay

I m trying to chain animation events. The application I m coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "...

热门标签