nsdate

Compare two NSDates

余生长醉 提交于 2019-12-21 12:42:28
问题 How can I compare two NSDates and return the amount of time between them. For example: 2010-06-20 14:29:41 -0400 2010-06-20 14:53:29 -0400 should return something like: 23 or 24 (minutes), rounding the seconds because I don't really need the seconds. 回答1: try something like: int intervall = (int) [date1 timeIntervalSinceDate: date2] / 60; 回答2: Here the complete snippet to compare two NSDate which you may find more useful based on your query. NSCalendar *myCal = [[NSCalendar alloc]

NSDateFormatter issue involving week numbers

六眼飞鱼酱① 提交于 2019-12-21 10:51:21
问题 On my machine (regional settings United States), the default "short" date format is set to "1/5/13" (month/day/year). In the System Preferences, I have appended to it a week number, "1/5/13 1". My problem is this code, where I try to convert a string to a date: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; NSDate *date = [dateFormatter dateFromString:@"1/5/13 1"]; NSLog(@"Date: %@", date); On my machine, this prints:

NSDate from Swift 2.3 to Date from Swift 3 Conversion

♀尐吖头ヾ 提交于 2019-12-21 09:28:34
问题 So I've just updated to Xcode 8 and converted my Swift 2.3 code to Swift 3, and Swift 3 just converted all NSDate to Foundation.Date And I had an extension for NSDate , now called Foundation.Date , it looked like this: extension NSDate { var firstDayOfTheMonth:NSDate { var date: NSDate? NSCalendar.currentCalendar().rangeOfUnit(.Month, startDate:&date , interval: nil, forDate: self) return date! } } And Swift 3 converted it to this: extension Foundation.Date { var firstDayOfTheMonth:Foundation

iOS CoreLocation checking CLLocation timestamp to use it

回眸只為那壹抹淺笑 提交于 2019-12-21 05:51:46
问题 How do you check a CLLocation object and decide whether you want to use it or discard the result and get a new location update instead? I saw the timestamp property on CLLocation, but I'm not sure how to compare that to the current time. Also, after I do compare the time and find the difference in seconds, what value should the difference be under for me to use that CLLocation object? What's a good threshold. Thanks! 回答1: Is really important to check the timestamp, because iOS usually caches

Check if system time is auto or user set

纵然是瞬间 提交于 2019-12-21 05:22:25
问题 I need to set up user proof time keeping in my current project. I have found a lot of different question around this, but none that seem to have the answer I am looking for. These are the questions i have looked at so far: XCODE: How to get/verify ACCURATE timestamp from device Is it possible to get the atomic clock timestamp from the iphone GPS? How can I get the real time in iPhone, not the time set by user in Settings? I have several options for getting a time from a server connection but

Format date string with .000Z into NSDate

一曲冷凌霜 提交于 2019-12-21 04:55:13
问题 I would like to format a date string into a NSDate object, which doesn't sound like a big thing. The point is, that the date string contains a dot in the timezone value instead of a plus or something else. A date looks like this: 2017-06-04T16:00:00.000Z I tried format strings like yyyy-MM-dd'T'HH:mm:ss.ZZZZ yyyy-MM-dd'T'HH:mm:ss.ZZZ yyyy-MM-dd'T'HH:mm:ss.Z Of course I've also checked it on nsdateformatter.com, which works but in xCode the NSDate is always nil. 回答1: This work for me var str =

Objective-C: Unicode Date Format

筅森魡賤 提交于 2019-12-21 04:48:21
问题 I am trying to work out how to have the UNICODE representation of Sun, 03 May 2009 19:58:58 -0700 as eee, dd MMM yyyy HH:mm:s ZZZZ or something. I can't seem to get this working precisely. 回答1: Use an NSDateFormatter. It lets you set a particular format string, using the format specifiers from the Unicode spec, then get the formatted date from a given NSDate object using stringFromDate:. Also consider reading Apple's doc about formatting dates. Example: // Given some NSDate *date

NSDateFormatter in 12-hour mode

 ̄綄美尐妖づ 提交于 2019-12-21 03:13:48
问题 I have the following code. NSDateFormatter *df = ...; [df setTimeZone:[NSTimeZone defaultTimeZone]]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; NSDate * date = [df dateFromString:date_string]; //here is the problem In 24-hour mode everything is ok. When 12-hour mode is set on device, stringFromDate returns null. Format of date_string is the same all the time, date format too. Why does it happen? 回答1: Try to set the locale in this way : NSLocale *twelveHourLocale = [[NSLocale alloc]

NSDateFormatter in 12-hour mode

南笙酒味 提交于 2019-12-21 03:13:05
问题 I have the following code. NSDateFormatter *df = ...; [df setTimeZone:[NSTimeZone defaultTimeZone]]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; NSDate * date = [df dateFromString:date_string]; //here is the problem In 24-hour mode everything is ok. When 12-hour mode is set on device, stringFromDate returns null. Format of date_string is the same all the time, date format too. Why does it happen? 回答1: Try to set the locale in this way : NSLocale *twelveHourLocale = [[NSLocale alloc]

How to turn a NSString into NSDate?

不想你离开。 提交于 2019-12-21 02:54:06
问题 Ive been racking my brains with no luck. Could someone please tell me how i would convert this string: "2011-01-13T17:00:00+11:00" into a NSDate? 回答1: The unicode date format doc is here Also, for your situation, you could try this: // original string NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"]; // convert to date NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; // ignore +11 and use timezone name instead of seconds from gmt [dateFormat setDateFormat: