nsdate

How do I create the current date (or any date) as an NSDate without hours, minutes and seconds?

故事扮演 提交于 2019-11-26 21:25:42
问题 I need to create an NSDate of the current date (or on any NSDate) without the hours, minutes and seconds and keep it as an NSDate , in as few lines of as possible? I need to perform some calculations on dates and having hours, minutes and seconds cause problems, I need absolute dates (I hope absolute is the right phrase). Its for an iPhone app. I'm creating dates with [NSDate date] which add hours, minutes and seconds. Also I'm adding months to dates which I think caters for day light savings

DateFormatter's method date(from: String) returns nil for specific dates in specific languages in Swift

不羁的心 提交于 2019-11-26 20:55:43
I really don't get what is happening with this code, I'm trying to convert a string to date. What I don't understand is that the conversion works for most of the dates, but doesn't work specially for only 2 dates. let dateFormatter = NSDateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: "es") dateFormatter.dateFormat = "dd 'de' MMMM" dateFormatter.dateFromString("1 de octubre") dateFormatter.dateFromString("2 de octubre") dateFormatter.dateFromString("3 de octubre") dateFormatter.dateFromString("4 de octubre") dateFormatter.dateFromString("5 de octubre") dateFormatter

How Do I write a Timer in Objective-C?

筅森魡賤 提交于 2019-11-26 19:47:10
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. 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 ): Output: Total time was: 0.002027 milliseconds Total time was: 0.000002 seconds Total time was: 0.000000

Comparing NSDates without time component

余生颓废 提交于 2019-11-26 19:43:28
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 ignores the time element? For instance I don't want a result if they are the same day, but different

Get day of week using NSDate

北城余情 提交于 2019-11-26 19:42:09
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(calendarIdentifier: NSGregorianCalendar) var myComponents = myCalendar.components(NSCalendarUnit

Measure elapsed time in Swift

老子叫甜甜 提交于 2019-11-26 19:30:21
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(number)) { println("Prime number"); } else { println("NOT a prime number"); } JeremyP Here's a Swift

NSPredicate: filtering objects by day of NSDate property

谁都会走 提交于 2019-11-26 19:24:22
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 this your recommendation, or is there something simpler? Given a NSDate * startDate and endDate and a

How do I get the current date in Cocoa

早过忘川 提交于 2019-11-26 18:58:20
问题 I'm getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I'm trying to create a countdown until midnight. To get the number of hour, minutes, and seconds, I do the following (which I found somewhere): NSDate* now = [NSDate date]; int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay]; int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour]; int sec

RKValueTransformers “failed transformation of value to NSDate”

痞子三分冷 提交于 2019-11-26 18:39:51
问题 I'm using RestKit, and pulling in a RSS feed. I'm getting this error for each date related to RKValueTransformers saying (for example): "Failed transformation of value 'September 04, 2014 09:58:01 PDT' to NSDate: none of the 10 value transformers consulted were successful." It goes on to tell me: Failed transformation of value at keyPath 'pubDate.text' to representation of type 'NSDate' and: {NSLocalizedDescription=Input value is not a valid ISO 8601 string: 'September 01, 2014 14:53:30 PDT'}

Why NSDate is reporting the wrong date?

五迷三道 提交于 2019-11-26 18:36:14
问题 Here is my code. NSDate *today = [NSDate date]; NSString *todayString = [[today description] substringToIndex:10]; NSLog(@"Today: %@", todayString); I am getting 2011-07-19 instead of 2011-07-18. Any ideas on what may be the problem? 回答1: NSDate's description method returns times in UTC, which if you are in the US Eastern Time Zone during daylight savings time is 4 hours later than your wall clock time. In other words, at 10pm your time it is 2am the next day in UTC. The usual way to fix it