nsdate

2 NSDates that should be equal aren't?

不羁的心 提交于 2019-12-03 20:10:15
I'm using the JSON library from Stig Brautaset(http://code.google.com/p/json-framework) and I need to serialize an NSDate. I was considering converting it into a string before JSONifying it, however, I ran into this weird behavior: Why aren't these NSDates considered equal? NSDate *d = [[NSDate alloc] init]; NSDate *dd = [NSDate dateWithString:[d description]]; NSLog(@"%@", d); NSLog(@"%@", dd); if( [d isEqualToDate:dd] ){ NSLog(@"Yay!"); } When you describe the original date object you lose some sub-second precision from the original object — in other words, -description shaves off fractional

DateTime to NSDate

泪湿孤枕 提交于 2019-12-03 18:17:41
How do I convert string 2010-11-19T20:00:00Z into an NSDate object? I've tried using [dateFormatter setDateFormat:@"yyyy-MM-ddTHH:mm:ssZ"] but it looks like I got the wrong custom format style. PS: I don't care about Timezones. Actually, I want the date to stay in the original (UTC) timezone. Try using your format string to convert from an NSDate to a string and see what you get. It's easier debugging in that direction than the other. In this case, looks as though you need to escape the T and Z literals in your pattern. Something like this: [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss

Get all days of any month with objective-c

拈花ヽ惹草 提交于 2019-12-03 17:57:36
问题 A seemingly simple question...how can I return a list of days for any specified month? NSDate *today = [NSDate date]; //Get a date object for today's date NSCalendar *c = [NSCalendar currentCalendar]; NSRange days = [c rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:today]; I basically want to use that, but replace today with say, the month of January, so I can return all of those days 回答1: Carl's answer works on Mac. The following works on Mac or iPhone (no

Check if system time is auto or user set

孤街醉人 提交于 2019-12-03 16:50:56
I need to set up user proof time keeping in my current project. I have found a lot of different question around this, but none that seem to have the answer I am looking for. These are the questions i have looked at so far: XCODE: How to get/verify ACCURATE timestamp from device Is it possible to get the atomic clock timestamp from the iphone GPS? How can I get the real time in iPhone, not the time set by user in Settings? I have several options for getting a time from a server connection but I need to have an offline solution too. 1. It seems that using CLLocation gets the same time as as the

List of all American holidays as NSDates

南楼画角 提交于 2019-12-03 15:03:31
I'm seeking a way to get all American holidays as an array of NSDate s. Is there a way to implement that? Here you'll find RSS-feeds with the holidays. It doesn't list year, but in the docs you'll find information how to change date ranges. Download it. I would suggest ASIHTTPRequest for that task. parse the RSS-feed. you can do so by normal XML-parsing, or you use a specialized parser. MWFeedParser would be one option. Save the dates. either by using CoreData, or Event Kit Framework If you only want US federal Holidays, I wrote this method. You could use these techniques to calculate any

Objective-C: Unicode Date Format

為{幸葍}努か 提交于 2019-12-03 14:18:42
I am trying to work out how to have the UNICODE representation of Sun, 03 May 2009 19:58:58 -0700 as eee, dd MMM yyyy HH:mm:s ZZZZ or something. I can't seem to get this working precisely. Use an NSDateFormatter . It lets you set a particular format string, using the format specifiers from the Unicode spec , then get the formatted date from a given NSDate object using stringFromDate: . Also consider reading Apple's doc about formatting dates . Example: // Given some NSDate *date NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"eee, dd MMM

WKInterfaceTimer used as a timer to countdown start and stop

和自甴很熟 提交于 2019-12-03 13:57:16
I am trying to create a timer to countdown x minutes and y seconds. I am computing the number of seconds and creating the InterfaceTimer like this: timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue+1))) timer.stop() after that I keep stoping it and starting it again and again, but the values are suddenly decreasing as "time(now) doesn't stop". Eg: if the timer shows :55, I start it for 3sec and stop it, it shows :52, I wait 10seconds and then start it again, it starts from :42. I can not save the value currently in the WKInterfaceTimer, so that I could start again from the same

iphone NSDate Conversion problem

女生的网名这么多〃 提交于 2019-12-03 13:54:52
问题 In my graph Api for facebook.. I am getting this data.. from Json.. "updated_time" = "2011-05-17T14:52:16+0000"; and I am using this code to convert it into valid date format NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; //2010-12-01T21:35:43+0000 [df setDateFormat:@"yyyy-mm-ddHH:mm:ssZZZZ"]; NSDate *date = [df dateFromString:[[forTable valueForKey:@"updated_time"] stringByReplacingOccurrencesOfString:@"T" withString:@""]]; [df setDateFormat:@"eee MMM dd, yyyy hh:mm"];

Initialize a NSDate object with a specific time

让人想犯罪 __ 提交于 2019-12-03 13:12:50
I have time in string format for example 2:00 . I want to initialize it to NSDate with present date . I tried doing NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setHour:2]; I am not able to compare this with a date object. Please help Try NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:6]; [comps setMonth:5]; [comps setYear:2004]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [gregorian dateFromComponents:comps]; You should be able to compare with date after that. Maybe you mean

How can I set only time in NSDate variable?

孤者浪人 提交于 2019-12-03 12:13:54
I have NSDate variable and would like to change only time (date shouldn't be changed). Is it possible ? Eg: user pick date interval in DatePicker date (If it's start date I would like to set time as 00:00:00, if it's end date, I set time as 23:59:59) Thanks all for your help. Regards, Alex. You'll want to use NSDateComponents . NSDate *oldDate = datePicker.date; // Or however you get it. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDate