nsdate

iPhone NSDate eg. next Friday

人走茶凉 提交于 2019-12-01 06:18:54
问题 I want to create a function which results the date of next Friday but I have no plan how to do it. Has anyone a good hint to me ? 回答1: E.g. Get current date using NSDate, then use 'components>fromDate:' from NSCalendar to get the NSDateComponents, then add the time difference to next Friday and create a new NSDate and Bob's is your uncle. 回答2: Here is my working solution for getting the next 5 Sundays in Gregorian calendar: self.nextBeginDates = [NSMutableArray array]; NSDateComponents

dynamically create date for previous sunday at 12:00 AM

左心房为你撑大大i 提交于 2019-12-01 06:05:10
问题 I'm struggling to figure out how to dynamically create a date object for the most previous sunday at 12:00 AM I was thinking I could get today's date and then subtract the current day of the week + 1 at which point I could just subtract the time of the day do get down to 12AM. so far I have the current day of the week: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:

UIDatePicker and NSDate

狂风中的少年 提交于 2019-12-01 06:05:00
问题 I have an NSDate that I get from a UIDatepicker. IBOutlet UIDatePicker *dueDate; NSDate *selectedDate = [dueDate date]; How do I retrieve the month from dueDate in the form of an int ? (1 for Jan, 2 for Feb, etc.) 回答1: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]]; NSInteger month = [components month]; 回答2: use -[NSCalendar components:fromDate:] for instance the example here: http:/

Setting NSDateComponents results in incorrect NSDate

ⅰ亾dé卋堺 提交于 2019-12-01 05:00:49
问题 I'm trying to get an NSDate object that has 21:00 as the local time - don't care about what day. I'm scratching my head at this very strange result: NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setHour:21]; [components setMinute:0]; [components setSecond:0]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"%@", date); The result is 0001-01-02 04:52:58 +0000

Sort NSDictionary keys as NSDate

自古美人都是妖i 提交于 2019-12-01 04:32:32
问题 I'm developing an iOS 4 application with latest SDK and XCode 4.2. I have a NSMutableDictionary where keys are NSDate and values are NSMutableString . When I have filled up the NSMutableDictionary I want to sort its keys but I don't know how can I do it. I want first January dates, then February and so on. The NSDate key format is dd/mm/yyyy . How can I sort those keys when NSMutableDictionary has all its keys and values? (I'm not going to add more). 回答1: That's a pretty standard thing,

format a NSDate to DDMMYYYY?

徘徊边缘 提交于 2019-12-01 04:27:49
问题 I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; Do I have to use the [dateComponents day] and so on? is there some method that can help me to performing that task like in PHP? 回答1

How can I get the actual date and time if the device date and time are inaccurate?

人盡茶涼 提交于 2019-12-01 04:18:48
I noticed that if I set my device time manually, and turn off the automatic time sync on my iOS device, [NSDate date] returns the date and time assuming the device time is correct--which it may not be. Since the docs mention that NSDate has some sort of sync with NTP, I am wondering if there is any built-in way to get the accurate date and time instead of having to assume the device date and time is correct. Perhaps https://github.com/jbenet/ios-ntp can help you? It's a library that can fetch the date and time from a time server. I tried ios-ntp but didn't get expected results, so I found

Converting date between timezones swift

倖福魔咒の 提交于 2019-12-01 02:57:25
I have a date stored on my online server database which is in GMT . I load the date and convert it to the user's timezone using the following code : if let messagedate = oneitem["timestamp"] as? String { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let date = dateFormatter.dateFromString(messagedate) let source_timezone = NSTimeZone(abbreviation: "GMT") let local_timezone = NSTimeZone.systemTimeZone() let source_EDT_offset = source_timezone?.secondsFromGMTForDate(date!) let destination_EDT_offset = local_timezone.secondsFromGMTForDate(date!) let time

Swift - Get local date and time

▼魔方 西西 提交于 2019-12-01 02:16:34
How can I get local date and time in Swift? let last_login = String(NSDate.date()) update: Xcode 8.2.1 • Swift 3.0.2 You can also use the Date method description(with locale: Locale?) to get user's localized time description: A string representation of the Date, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from UTC (for example, “2001-03-24 10:45:32 +0600”). description(with locale: Locale?) Date().description(with: .current) // "Monday, February 9, 2015 at 05:47

How can I convert the 24 hour time format to 12 hour time format?

纵然是瞬间 提交于 2019-12-01 01:57:05
I can get the 24 hour style time, but how do I get the 12 hour format with the am and pm? let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Hour, .Minute, .Second], fromDate: date) let hour = components.hour let minutes = components.minute As you say, you should use NSDateFormatter for your desired format. Hope this will help you. let date = NSDate() let formatter = NSDateFormatter() formatter.dateFormat = "hh:mm a" let time = formatter.stringFromDate(date) For more information about date formatter see NSDateFormatter Swift 3.1 Answer let