nsdate

IOS: problem with NSOrderedDescending and NSOrderedAscending

落爺英雄遲暮 提交于 2019-12-05 21:06:16
I want to compare dates and I use this code in one example date values for my date are: date1 = 6/06/2011 date2 = 8/06/2011 if dateSelected = 7/06/2011 it's all ok, but if dateSelected = 6/06/2011 or dateSelected = 8/06/2011 the code don't entry inside my "if", why??? if (([dateSelected compare:date1] == NSOrderedDescending) && ([dateSelected compare:date2]== NSOrderedAscending)) {} NSDates represent a point in time since 1/1/2000 00:00 UTC. So all of those dates have time components. -compare: compares date and time. Presumably, you really want to check if the date selected is between 6/6

Calculating first and last days of current week

こ雲淡風輕ζ 提交于 2019-12-05 19:50:23
Reference to pre-answered question at: Getting first and last days of current week There are two answers in the above link. One of them is theoretical and the other is in a language called as PyObjC (Python-Objective C bridge language), and a quick google search confirms that PyObjC does not work with iPhone SDK. So regarding the question, how is it possible to get the PyObjC code translated to be compatible with iPhone SDK. Target: Supposing today (Tue.) is 19th, and Sun. was 17th (start of week) and Sat. 23rd is end of week. I want to get a string like 19/01 - 23/01 [i.e. The start of week

Date formatter returns nil for June

会有一股神秘感。 提交于 2019-12-05 18:58:02
问题 I have a strange problem with those lines of code : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"062008"]; NSLog(@"Date %@", date); The result is : Date (null) But when I change the month like this : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"072008"]; NSLog(@"Date %@", date); The result is : Date 2008-06-30 23:00:00 +0000 For only the

NSDictionary with NSDates as keys

心已入冬 提交于 2019-12-05 18:32:30
Is it possible to have an NSdictionary where the keys are NSDates and also have it writeable/archiveable to disk? Jens Ayton Not using property lists, as only string keys are allowed there. You should be able to write it using NSKeyedArchiver , but that has the disadvantage of being a more opaque format. Alternatively, you could of course make a copy of the dictionary where you convert the dates to strings. Yes, this should work just fine - and NSDate elements are also suitable for plist encoding. Might depend on the value you put in as values, though. If you experience problems, please update

How to convert NSDate to milliseconds in Objective-C? [duplicate]

。_饼干妹妹 提交于 2019-12-05 18:16:13
问题 This question already has answers here : How to get nsdate with millisecond accuracy? (3 answers) Closed 6 years ago . For example: assume NSDate tempdate = "05-23-2013" . I want this value to be converted into milliseconds. How can I do this with Objective-C? 回答1: There is a similar post here and here. Basically you have get the second form the reference date (1 January 2001, GMT) and multiply it with 1000. NSTimeInterval seconds = [NSDate timeIntervalSinceReferenceDate]; double milliseconds

NSDate/NSDateFormatter - Storing only time, not date?

有些话、适合烂在心里 提交于 2019-12-05 18:14:21
问题 I've been looking around but I haven't seen anything that addresses this so I'm hoping someone can help clear this up for me. What I am trying to do is use an NSDate variable(in core data) to store a time, not date and time, but just time in the format HH:MM:SS; After looking at the NSDateFormatter class reference and the sample code provided I was able to tweak it and think that my code should look something like the following: NSDateFormatter *timeOfArrival = [[NSDateFormatter alloc] init];

NSDate to RFC 2822 Date format

懵懂的女人 提交于 2019-12-05 16:54:29
Is there any efficient way to convert an NSDate to RFC 2822 Date format string ? I want to use this string to create an NSURLRequest and set the value of "If-Modified-Since" header field. try to use an NSDateFormatter NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // maybe there exist a new-method now dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss Z"; //RFC2822-Format NSString *dateString = [dateFormatter stringFromDate:myDate]; Swift 3 let rfcDateFormat = DateFormatter() rfcDateFormat.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z" let dateSTring = rfcDateFormat.string

iOS - how to compare two times?

孤街浪徒 提交于 2019-12-05 15:27:25
I use the following function to check if a message has expired - - (BOOL) hasExpired:(NSDate*)myDate { if(myDate == nil) { return false; } NSDate *now = [NSDate date]; return !([now compare:myDate] == NSOrderedAscending); } This works fine if I am comparing two different dates. However, it returns false if the message has expired earlier in the day today. Any ideas on how I might fix it? (Adding my comment as an answer:) This should not happen, also 1 second difference between NSDate instances is enough. Add an NSLog() with both dates there to see whether they are different indeed. You can use

How can I get the “internet time” on iOS? [duplicate]

妖精的绣舞 提交于 2019-12-05 13:13:31
This question already has an answer here: Is there any way to get the tamper-proof date and time on iPhone? 1 answer So, I'm developing an app that is based on the time, but with NSDate an user can easily hack the application by changing system time of the device. So I might want to use a better method for retrieving time. How can I do this? Is there an easy way to retrieve time from internet? Ben-G Checkout this Google Library: https://github.com/jbenet/ios-ntp After the library is set up you can use it with the following line: [NSDate networkDate]; A similar question has also been answered

NSDate as keys for NSDictionary

依然范特西╮ 提交于 2019-12-05 12:37:46
Is it possible to add NSDate as keys and some arrays as it values in a NSDictionary? I dont have any requirement of writing the dictionary to disk - archiving or unarchiving, just as this guy needs it: NSDictionary with NSDates as keys But, why I want NSDate to be added as keys specifically is so that I can fetch all keys from the dictionary and do some computations like, fetching the dates from within a range. Whether two objects with same date value share same memory when created? Is it possible to have NSDate as key? Also, what other problems I might face with this assumption? Thanks, Raj