nsdate

Swift 3.0 : Convert server UTC time to local time and vice-versa

帅比萌擦擦* 提交于 2019-11-28 03:59:15
I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString.index(timeString.startIndex, offsetBy: 5) let twelve = timeString.substring(to: index) var dateString:String! let dateFormatter = DateFormatter() dateFormatter.dateFormat = "H:mm" let date12 = dateFormatter.date(from: twelve)! dateFormatter.dateFormat = "h:mm a" let date22 = dateFormatter.string(from: date12) //print(date22) dateString = date22 //print(

Find difference in seconds between NSDates as integer using Swift

独自空忆成欢 提交于 2019-11-28 03:31:40
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") var elapsedTime = NSDate.timeIntervalSinceDate(timeAtPress) duration = ??? } I've seen a few similar

How to get NSDate day, month and year in integer format?

偶尔善良 提交于 2019-11-28 03:31:23
I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer. How can I do this in iOS? I found the similar question but the method descriptionWithCalendarFormat : gives a warning and seems to be deprecated by now. Janak Nirmal Here you are, NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components

NSPredicate BETWEEN with NSDate causes -[__NSDate constantValue]: unrecognized selector sent to instance 0x1e7ff0

删除回忆录丶 提交于 2019-11-28 03:31:22
问题 I'm trying to fetch from Core Data records that have a startTime between two dates. Here's my code: NSDate *today = [NSDate date]; NSDate *distantFuture = [NSDate distantFuture]; // Create the predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startTime BETWEEN %@", [NSArray arrayWithObjects:today, distantFuture, nil]]; NSLog(@"today: %@, distantFuture: %@", today, distantFuture); NSLog(@"predicate: %@", predicate); // Add the predicate to the fetchRequest [[[self

Difference between NSDate and NSDateComponent value for the same date

*爱你&永不变心* 提交于 2019-11-28 02:23:41
I created a simple function to get first and last day of a week for a day in it. Looking at the NSLog output i found that different values are returned from a NSDate descriptor and component day for the same date, why ? Here NSLog outputs: NSDATE: 2011-04-03 22:00:00 +0000, DAY COMPONENT: 4 NSDATE: 2011-04-09 22:00:00 +0000, DAY COMPONENT: 10 As you can see, NSDATE is 3 of April and day component is 4 for the first row, and respectively 9 and 10 for the second one. Here the code: NSDate *date = [NSDate date]; //Today is April 5th 2011 NSCalendar *cal =[[NSCalendar alloc

stringFromDate returning the wrong year

自作多情 提交于 2019-11-28 02:02:15
#define stdDateFormat @"YYYYMMdd'T'hh:mm:ssZ" NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; [dateFormat setDateFormat:stdDateFormat]; NSString *TimeOfSync = [dateFormat stringFromDate:syncDate]; NSLog(@"date format: %@",stdDateFormat); NSLog(@"syncDate: %@",syncDate); NSLog(@"TimeOfSync: %@",TimeOfSync); Logged output: date format: YYYYMMdd'T'hh:mm:ssZ syncDate: 2009-01-03 19:00:00 +0000 TimeOfSync: 20080103T11:00:00-0800 Can anyone help? The only thing I can see is the extra space just before the timezone in the input date string. Thanks. Use lowercase yyyy for the year instead

iOS NSDate() returns incorrect time

怎甘沉沦 提交于 2019-11-28 01:46:27
I am trying to get current date in Swift using NSDate() . When I create breakpoint and stop application, I can see that there is 3 hours difference with devices' system time. Application is running on real iPhone. How to fix this? I also wrote the following in App Delegate to be sure: NSTimeZone.setDefaultTimeZone(NSTimeZone(name: "Europe/Kiev")!) But it still does not work. It doesn't return the wrong time. It returns exactly the right time. NSDate doesn't have any timezone information. Right now, my computer and your computer will report the exact same time when we call NSDate (). NSLog

iOS NSDate Comparison works differently when the 24-Hour Time in date settings toggles between ON and OFF?

谁都会走 提交于 2019-11-28 01:32:40
Team, I am comparing the date which is formed from string using NSDateFormatter with the iOS system date. The below statement returns true when the system date time settings is set with 24-Hour Time ON, but the same code returns false when 24-Hour Time OFF. Problematic Code: if ([(NSDate*)[NSDate date] compare:currDate] == NSOrderedAscending) { // -- Code -- This is executed only when the 4-Hour Time ON } I am confused. The string using which I am getting the date is in 24 hours format. Is this a problem? Or anything else? Date Formatting Code: -(NSDate *)getDateFromString:(NSString *

Get the Week Number in iOS SDK

牧云@^-^@ 提交于 2019-11-28 01:09:47
问题 I want to get the Week number of the year from the date.I tried the code as follow but gives me a wrong week number. My code for week number: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY-MM-dd"]; NSDate *date = [dateFormatter dateFromString:@"2012-09-15"]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSLog(@"week: %i", [[calendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear]); //Display 38 instead of 37 } Note:

Programmatically getting the date “next Sunday at 5PM”

对着背影说爱祢 提交于 2019-11-28 00:16:00
Edited 07/08/13: Apple has an excellent set of WWDC videos that really helped me understand the various date and time classes in Objective-C, and how to correctly perform time calculations/manipulations with them. "Solutions to Common Date and Time Challenges" ( HD video , SD video , slides (PDF) ) (WWDC 2013) "Performing Calendar Calculations" ( SD video , slides (PDF) ) (WWDC 2011) Note: links require a free Apple Developer membership. I'm writing an app for a friend's podcast. She broadcasts her show live every Sunday at 5PM, and I would like to write some code in my app to optionally