nsdate

Notification in Swift every day at a set time? [duplicate]

萝らか妹 提交于 2019-12-05 11:54:09
This question already has an answer here : UILocalNotification always shows up as soon as I load my app regardless of the fire date (1 answer) Closed 4 years ago . If anyone gets confused and thinks that this is a duplicate of my question from yesterday, it's not. There I was asking how to call a function every day, here I am asking how to call a notification at a specific time every day. I am looking for a way to repeat a local notification every day at 7.00AM. I currently have this code setup to get the day, month, year etc. let date = NSDate() let calendar = NSCalendar.currentCalendar() let

Whither NSDate dateByAddingTimeInterval on iPhone OS?

帅比萌擦擦* 提交于 2019-12-05 11:23:42
Greetings! I must be seeing things. Look at this excerpt from the iPhone OS reference library: addTimeInterval: Returns a new NSDate object that is set to a given number of seconds relative to the receiver. (Deprecated. This method has been replaced by dateByAddingTimeInterval: .) However, it is nowhere to be found in the docs, nor in the headers. If I look at the Mac OS SDK, then I find it. Typo? Just keep using addTimeInterval: after all?? It's actually an error in the docs. addTimeInterval: is deprecated in Mac OS X 10.6 but not in iPhone OS 3.1.2. You can look at the NSDate.h in MacOS and

Parse JSON date into NSDate depending user settings

隐身守侯 提交于 2019-12-05 10:00:47
问题 I have a problem for converting a brut JSON string date: "created_at" = "2012-12-22T21:39:22Z"; into an NSDate. Here is my current category for that: - (NSDate*)dateWithJSONString { [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; [dateFormatter setCalendar:[[NSCalendar alloc]

Convert NSDate to String with strftime

岁酱吖の 提交于 2019-12-05 08:58:56
How would I convert an NSDate to a NSString, formatted with strftime specifiers? you could use strftime. NSDate *date = [NSDate date]; time_t time = [date timeIntervalSince1970]; struct tm timeStruct; localtime_r(&time, &timeStruct); char buffer[80]; strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", &timeStruct); NSString *dateStr = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]; I hope it's correct. You'll need an NSDateFormatter , using setDateFormat: . Here's the documentation . 来源: https://stackoverflow.com/questions/5008876/convert-nsdate-to-string-with-strftime

How do I subtract a duration from an NSDate, but not include the weekends?

爷,独闯天下 提交于 2019-12-05 08:15:54
Using today as an example, how do I determine which date it was, 230 workdays ago? I know how to do it iteratively with a while loop checking date and subtracting 1 if it's a workday, but I'm wondering if there is a better method. Also, let's take a Sunday 1 PM as an example, and subtract 3 work days and 2 hours from that time. First, it doesn't make sense to subtract work-time from weekends. So it would have to move the time to 23:59:59 of Friday, and then subtract those 3 days and 2 hours. If it's a Monday at 1:30 AM, and I'm subtracting 5 days and 3 work-hours from that time, then the

Convert date to next day's date in iOS

流过昼夜 提交于 2019-12-05 07:07:08
问题 I have a date in string format just like 30-11-2012 . I want the date next to it like 01-12-2012 again in string format. Is there any way of getting it? 回答1: You should use NSCalendar for best compatibility NSDate *today = [NSDate dateWithNaturalLanguageString:@"2011-11-30"]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *dayComponent = [NSDateComponents new]; dayComponent.day = 1; today = [calendar dateByAddingComponents:dayComponent toDate:today options:0]; //2011-12

Sort NSDate in order while ignoring time

…衆ロ難τιáo~ 提交于 2019-12-05 06:15:37
问题 I want to sort an array of NSDates only on their day month and year so not on any of the units of time. How can I do this because I can't see a way of getting the individual parts of an NsDate "out". 回答1: See this post for sorting an array containing NSDate objects: Sort NSArray of date strings or objects and mine for removing the time: Truncate NSDate (Zero-out time) ADDITION 1) Loop through your array of NSDate objects, removing (zeroing) the time components, and adding them to a new array:

iOS Countdown Timer To Specific Date

北城余情 提交于 2019-12-05 06:07:48
问题 I am trying to get a countdown timer to a specific date. I use: -(void) viewWillAppear:(BOOL)animated { NSString *str =@"12/27/2013"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"MM/dd/yyyy"]; NSDate *date = [formatter dateFromString:str]; NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow]; NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400; startTime = [[NSDate date] retain]; secondsLeft = numberOfDays; NSLog

How can I format two NSDate as a time difference string (e.g. 2days ago) and make it follow the regional formatting of the device?

无人久伴 提交于 2019-12-05 04:55:31
问题 Does the standard framework support time difference formatting and create a format that follows the regional settings? I know I can break it to NSDateComponents but then I will have to append the text and create different language support files myself. I'm wondering that there may be a way to formatting the date and make it follows the regional setting simple and similar to this... dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateStyle:NSDateFormatterMediumStyle]

Find NSDate for the next closest specific day of week for any date

不问归期 提交于 2019-12-05 04:10:51
Let's suppose that today is Wednesday. I can break an NSDate down into NSDateComponents , but I need to find the NSDate with the next upcoming Monday. If today is Monday, then the next upcoming Monday is today. What's the right way to achieve this? You can use nextDateAfterDate: method on NSCalendar object to achieve this, let now = Date() // today var matchingComponents = DateComponents() matchingComponents.weekday = 2 // Monday let comingMonday = Calendar.current.nextDate(after: now, matching: matchingComponents, matchingPolicy:.nextTime) Here, is a simple method to find next monday. If