nsdate

Comparing NSDates without time component

只愿长相守 提交于 2019-11-26 08:02:41
问题 In a swift playground, I have been using NSDate.date() But, this always appears with the time element appended. For my app I need to ignore the time element. Is this possible in Swift? How can it be done? Even if I could set the time element to be the same time on every date that would work too. Also, I am trying to compare two dates and at the moment I am using the following code: var earlierDate:NSDate = firstDate.earlierDate(secondDate) Is this the only way or can I do this in a way that

NSDate - Convert Date to GMT

独自空忆成欢 提交于 2019-11-26 07:58:36
问题 I need the ability to convert an NSDate value to a GMT Date. How can I go about converting an NSDate value to a GMT formatted NSDate value, independent of whatever date locale settings the iPhone device is using? 回答1: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];

How can I calculate the difference between two dates?

余生颓废 提交于 2019-11-26 07:57:24
问题 How can I calculate the days between 1 Jan 2010 and (for example) 3 Feb 2010? 回答1: NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"]; NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"]; NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; int numberOfDays = secondsBetween / 86400; NSLog(@"There are %d days in between the two dates.", numberOfDays); EDIT: Remember, NSDate objects represent exact moments of time, they do not have any

Converting a string to an NSDate

狂风中的少年 提交于 2019-11-26 07:43:53
How is it possible to convert a string to an NSDate on iOS? NSString *dateStr = @"20100223"; // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyyMMdd"]; NSDate *date = [dateFormat dateFromString:dateStr]; // Convert date object to desired output format [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"]; dateStr = [dateFormat stringFromDate:date]; [dateFormat release]; Hope this will help you. You'll want to take a look at NSDateFormatter . Determine the format of the date string and then use dateFromString: to convert the

How Do I write a Timer in Objective-C?

人盡茶涼 提交于 2019-11-26 07:26:47
问题 I am trying to make a stop watch with NSTimer. I gave the following code: nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO]; and it is not working in milliseconds. It takes more than 1 millisecond. 回答1: Don't use NSTimer that way. NSTimer is normally used to fire a selector at some time interval. It isn't high precision and isn't suited to what you want to do. What you want is a High resolution timer class (using NSDate

Measure elapsed time in Swift

谁说胖子不能爱 提交于 2019-11-26 06:58:48
问题 How can we measure the time elapsed for running a function in Swift? I am trying to display the elapsed time like this: \"Elapsed time is .05 seconds\". Saw that in Java, we can use System.nanoTime(), is there any equivalent methods are available in Swift to accomplish this? Please have a look at the sample program: func isPrime(var number:Int) ->Bool { var i = 0; for i=2; i<number; i++ { if(number % i == 0 && i != 0) { return false; } } return true; } var number = 5915587277; if(isPrime

NSPredicate: filtering objects by day of NSDate property

北战南征 提交于 2019-11-26 06:55:59
问题 I have a Core Data model with an NSDate property. I want to filter the database by day. I assume the solution will involve an NSPredicate , but I\'m not sure how to put it all together. I know how to compare the day of two NSDate s using NSDateComponents and NSCalendar , but how do I filter it with an NSPredicate ? Perhaps I need to create a category on my NSManagedObject subclass that can return a bare date with just the year, month and day. Then I could compare that in an NSPredicate . Is

first and last day of the current month in swift

落花浮王杯 提交于 2019-11-26 06:28:13
问题 I\'m trying to get the first and last day of the month in swift. So far I have the following: let dateFormatter = NSDateFormatter() let date = NSDate() dateFormatter.dateFormat = \"yyyy-MM-dd\" let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date) let month = components.month let year = components.year let startOfMonth = (\"\\(year)-\\(month)-01\") But I\'m not sure how to get the last date. Is there a

Get day of week using NSDate

扶醉桌前 提交于 2019-11-26 06:27:13
问题 I created a method that is supposed to take in a string in \"YYYY-MM-DD\" form and spit out an int that represents the dates position in relation to the week it is in (regardless if it overlaps between months). So e.g sunday=1 monday=2 and so on. Here is my code: func getDayOfWeek(today:String)->Int{ var formatter:NSDateFormatter = NSDateFormatter() formatter.dateFormat = \"YYYY-MM-DD\" var todayDate:NSDate = formatter.dateFromString(today)! var myCalendar:NSCalendar = NSCalendar

How do I get an ISO 8601 date on iOS?

喜你入骨 提交于 2019-11-26 06:05:20
问题 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