nsdate

Data Formatters temporarily unavailable

你。 提交于 2019-11-26 18:27:05
问题 Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program received signal: “EXC_BAD_ACCESS”. Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib") 回答1: This is nothing to do with NSDateFormatter - the message pasted in saying "Data Formatters" is correct. You will get this message in several situations, possibly most commonly when unable to find a

Converting UTC date format to local nsdate

纵然是瞬间 提交于 2019-11-26 18:02:26
问题 I am getting from my server a string date in UTC time zone and I need to convert it to the local time zone. MY CODE: let utcTime = "2015-04-01T11:42:00.269Z" let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone(name: "UTC") dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" let date = dateFormatter.dateFromString(utcTime) println("utc: \(utcTime), date: \(date)") this prints - utc: 2015-04-01T11:42:00.269Z, date: Optional(2015-04-01 11:42:00 +0000) if I remove

How do you calculate the day of the year for a specific date in Objective-C?

一曲冷凌霜 提交于 2019-11-26 17:49:35
This is something I found myself spending hours to figure out and therefore want to share with you. The question was: How do I determine the day of the year for a specific date? e.g. January 15 is the 15th day and December 31 is the 365th day when it's not leap year. John Feminella Try this: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]]; [gregorian release]; return dayOfYear; where date is the date you want to determine the day of

NSDate of yesterday

六月ゝ 毕业季﹏ 提交于 2019-11-26 17:37:02
问题 How do I create an NSDate object with a custom date other than the current date? For example I would like to create a var of yesterday or of 2 days ago. 回答1: You should use NSCalendar for calculating dates. For example, in Swift 3 the date two days before today is: let calendar = Calendar.current let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date()) Or in Swift 2: let calendar = NSCalendar.currentCalendar() let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate:

How can I get an NSDate object for today at midnight?

蓝咒 提交于 2019-11-26 17:31:09
What is the most efficient way to obtain an NSDate object that represents midnight of the current day? Try this: 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]; New API in iOS 8 iOS 8 includes a new method on NSCalendar called startOfDayForDate , which is really easy to use: let

How to compare two NSDate objects in objective C

折月煮酒 提交于 2019-11-26 17:31:06
问题 I have objects of Date type. I want to compare them, and I have written an if condition in the following way: if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped)) { ///execute some condition } Am I right or wrong in this approach? One more thing. Is this way right: if (dtdate > [m_array objectatIndex:i] { } Because I am getting random behavior. 回答1: Here's an example using the NSDate method,

What's the optimum way of storing an NSDate in NSUserDefaults?

怎甘沉沦 提交于 2019-11-26 17:27:40
问题 There's two ways of storing an NSDate in NSUserDefaults that I've come across. Option 1 - setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"]; Option 2 - timeIntervalSince1970 // Set NSDate *myDate = [NSDate date]; NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970]; [[NSUserDefaults

NSDate not returning correct date

女生的网名这么多〃 提交于 2019-11-26 17:19:31
问题 I am printing NSDate like this: NSDate *date = [NSDate date]; NSString *stringDate = [data description]; Right now, it's July 1, 2011 11:43 pm. My iPod even says that on the top bar. But stringDate prints out: 2011-07-02 03:43:46 +0000 This is obviously wrong. I have used NSDate millions of times but never had this problem. What could be wrong? Thanks 回答1: Your region's time offset is -04:00 ? NSDate will automatically adjust the time offset when displaying the date. Try, NSString *str =

Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat

时间秒杀一切 提交于 2019-11-26 17:17:22
What setDateFormat option for NSDateFormatter do I use to get a month-day's ordinal suffix? e.g. the snippet below currently produces: 3:11 PM Saturday August 15 What must I change to get: 3:11 PM Saturday August 15 th NSDate *date = [NSDate date]; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateFormat:@"h:mm a EEEE MMMM d"]; NSString *dateString = [dateFormatter stringFromDate:date]; NSLog(@"%@", dateString); In PHP, I'd use this for the case above: <?php echo date('h:m A l F

How do I get an ISO 8601 date on iOS?

拟墨画扇 提交于 2019-11-26 17:12:32
It's easy enough to get the ISO 8601 date string (for example, 2004-02-12T15:19:21+00:00 ) in PHP via date('c') , but how does one get it in Objective-C (iPhone)? Is there a similarly short way to do it? Here's the long way I found to do it: NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; NSDate *now = [NSDate date]; NSString *formattedDateString = [dateFormatter stringFromDate:now]; NSLog(@"ISO-8601 date: %@", formattedDateString); // Output: ISO-8601 date: 2013-04-27T13:27:50-0700 It seems an awful lot of