nsdate

Find difference in seconds between NSDates as integer using Swift

元气小坏坏 提交于 2019-12-17 15:22:29
问题 I'm writing a piece of code where I want to time how long a button was held down. To do that I recorded an NSDate() when the button was pressed, and tried using the timeIntervalSinceDate function when the button was released. That seems to work but I can't find any way to print the result or switch it to an integer. var timeAtPress = NSDate() @IBAction func pressed(sender: AnyObject) { println("pressed") timeAtPress = NSDate() } @IBAction func released(sender: AnyObject) { println("released")

Does [NSDate date] return the local date and time?

送分小仙女□ 提交于 2019-12-17 13:06:30
问题 Am I stupid? All this time I thought [NSDate date] returned the local date and time. After having some trouble with NSStringformatter/stringFromdate/dateFromString today I noticed that my [NSDate date] was returning 2011-03-06 11:00:00 +0000. After researching this I see that [NSDate date] returns a raw date which is always GMT. What purpose does the gmt offset portion serve if it always shows +0000? Also I do not understand [myDate description]. The docs says it is supposed to display gmt

GMT timezone conversion in objective c

安稳与你 提交于 2019-12-17 10:33:31
问题 I am trying to convert nsstring to nsdate and then to the systemtimezone. Is my code right? Any help appreciated. NSString *str=@"2012-01-15 06:27:42"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *dateFromString = [dateFormatter dateFromString:str]; NSDate* sourceDate = dateFromString; NSTimeZone* sourceTimeZone = [NSTimeZone

get current date from [NSDate date] but set the time to 10:00 am

喜欢而已 提交于 2019-12-17 08:45:47
问题 How can I reset the current date retrieved from [NSDate date] but then change the time to 10:00 in the morning. 回答1: As with all date manipulation you have to use NSDateComponents and NSCalendar NSDate *now = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now]; [components setHour:10]; NSDate *today10am =

NSDate beginning of day and end of day

僤鯓⒐⒋嵵緔 提交于 2019-12-17 07:13:25
问题 -(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; return [cal dateFromComponents:components]; } -(NSDate *)endOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents

How to convert NSDate into unix timestamp iphone sdk?

冷暖自知 提交于 2019-12-17 07:01:24
问题 How to convert an NSDate into Unix timestamp? I've read many posts which do the reverse. But I'm not finding anything related to my question. 回答1: I believe this is the NSDate's selector you're looking for: - (NSTimeInterval)timeIntervalSince1970 回答2: A Unix timestamp is the number of seconds since 00:00:00 UTC January 1, 1970. It's represented by the type time_t, which is usually a signed 32-bit integer type (long or int). iOS provides -(NSTimeInterval)timeIntervalSince1970 for NSDate

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

跟風遠走 提交于 2019-12-17 03:42:40
问题 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")

swift NSDateFormatter not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 03:20:42
问题 I'm trying to format time as follows: let formatter = NSDateFormatter() formatter.dateFormat = "hh_mm_ss" let d = formatter.stringFromDate(NSDate()) println("formatted text is: \(d)") Suppose that current time is Sep 3, 2014 22:15:30 When I run this script on playground, it prints the time correctly formatted: 22_15_30 . When running this on AppDelegate, it doesn't print the time formatted: 22:15:30 I'm using xcode 6 beta 5... Am I missing something? Why stringFromDate doesn't return the

Comparing two NSDates and ignoring the time component

核能气质少年 提交于 2019-12-17 02:58:10
问题 What is the most efficient/recommended way of comparing two NSDates? I would like to be able to see if both dates are on the same day, irrespective of the time and have started writing some code that uses the timeIntervalSinceDate: method within the NSDate class and gets the integer of this value divided by the number of seconds in a day. This seems long winded and I feel like I am missing something obvious. The code I am trying to fix is: if (!([key compare:todaysDate] == NSOrderedDescending

How to compare two NSDates: Which is more recent?

五迷三道 提交于 2019-12-17 02:26:37
问题 I am trying to achieve a dropBox sync and need to compare the dates of two files. One is on my dropBox account and one is on my iPhone. I came up with the following, but I get unexpected results. I guess I'm doing something fundamentally wrong when comparing the two dates. I simply used the > < operators, but I guess this is no good as I am comparing two NSDate strings. Here we go: NSLog(@"dB...lastModified: %@", dbObject.lastModifiedDate); NSLog(@"iP...lastModified: %@", [self