nsdate

What's the optimum way of storing an NSDate in NSUserDefaults?

我们两清 提交于 2019-11-27 00:03:55
There's two ways of storing an NSDate in NSUserDefaults that I've come across. Option 1 - setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"]; Option 2 - timeIntervalSince1970 // Set NSDate *myDate = [NSDate date]; NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970]; [[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"]; // Get NSTimeInterval myDateTimeInterval = [

Difference between NSDate and NSDateComponent value for the same date

别等时光非礼了梦想. 提交于 2019-11-26 23:43:07
问题 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

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

会有一股神秘感。 提交于 2019-11-26 23:25:01
问题 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

Check if the time and date is between a particular date and time

核能气质少年 提交于 2019-11-26 23:24:07
问题 I have 3 variables; startDate - Wed Apr 12 endDate - Wed Jun 11 timeDuration - 11.00 am from 11.00 pm . (this is a string, 11AM is the start time and 12PM is the end time) 1.) Now, i need to get the current date and time ( [NSDate date] ) and check if the current date is between the startDate and endDate and then check if the current time is between the timeDuration (as in between 11.00am from 11.00pm ) 2.) If the current time is between the time duration (as in between 11.00 am from 11.00 pm

How to add a time interval to an NSDate?

删除回忆录丶 提交于 2019-11-26 23:12:43
问题 I have an NSDate and a duration. I need to get the time after the duration Given: The date is "2010-02-24 12:30:00 -1000" duration is 3600 secs I need to get "2010-02-24 13:30:00 -1000" I thought dateWithTimeIntervalSinceReferenceDate: , would do the trick but I see now that this gives a date offset from 1 Jan 2001 GMT. Is there another C function I need to use 回答1: As DyingCactus states, you can use the addTimeInterval method of NSDate, but depending on the OS version is will create a

NSDate timeIntervalSince1970 not working in Swift?

二次信任 提交于 2019-11-26 23:10:36
I am doing this in swift: let date = NSDate(timeIntervalSince1970: 1432233446145.0) println("date is \(date)") The log gives me this: date is 47355-09-02 23:55:45 +0000 Then I try to get an interval back out just for testing: let tI = expirationDate.timeIntervalSinceDate(date) println("tI = \(tI)") I get: tI = 0.0 What am I doing wrong? I can seem to make the timeIntervalSince1970 call work properly. Is there any known issued with that in Swift, or am I missing something here? 1432233446145 most probably is a time interval given in milliseconds : let date = NSDate(timeIntervalSince1970:

Limiting UIDatePicker dates from a particular time. Such as Input DOB to a restricted age limit

元气小坏坏 提交于 2019-11-26 22:58:52
In my code, I have a UITextField that when the user taps on opens a UIDatePicker to enable the user to easily and efficiently scroll through to their Date Of Birth. Obviously, we wouldn't want the UIDatePicker to scroll up to 2015 and over as it currently does. As it's a Date Of Birth entry field, i would also need to be able to limit entries to 16years+. How do i do this? class SignUpViewController: UIViewController, UITextFieldDelegate { var datePicker:UIDatePicker! @IBOutlet weak var dateTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() // UI DATE PICKER SETUP var

stringFromDate returning the wrong year

一笑奈何 提交于 2019-11-26 22:05:09
问题 #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

Programmatically getting the date “next Sunday at 5PM”

浪子不回头ぞ 提交于 2019-11-26 21:40:04
问题 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

NSDate - Convert Date to GMT

喜你入骨 提交于 2019-11-26 21:32:57
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? 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]]; [dateFormatter release]; Working with time in Cocoa can be complicated. When you get an NSDate object, it's