Note that there are some explanatory texts on larger screens.

plurals
  1. PODate difference between UIDatepicker and NSDate
    primarykey
    data
    text
    <p>I have a datepicker and I save the value to .plist file with key UserDate in string format.</p> <p>The value returns from datepicker is in <code>2013-02-13 11:17:34 +0000</code> format.</p> <p>In order to save it into to .plist file I need to convert datepicker to string <code>2013-02-14T19:17:34Z</code></p> <p>In other View, I want to retrieve back the UserDate from plist and compare with the user current date. I want days and hours different. </p> <p>I convert and pass <code>2012-02-12 19:17:34 +0800</code> as <code>destinationDate</code> to compare with [NSDate new]. I managed to calculate the difference between two date. But the result is not correct.</p> <h2>How to get the correct difference result for two dates between iPhone's date and Datepicker's date? Do I need to use a specific timezone?</h2> <p><strong>Extra info</strong>: I live in GMT+8. I set the simulator datepicker to Feb 13 2012, yet the result still say the countdown has 18hours and 4 minutes.</p> <pre><code>NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; [datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']]; </code></pre> <p>NSLog</p> <p><img src="https://i.stack.imgur.com/8iu7m.png" alt="enter image description here"></p> <p><strong>Edit</strong></p> <p>Plist file.</p> <p><img src="https://i.stack.imgur.com/yB12F.png" alt="enter image description here"></p> <p>save method</p> <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path NSString *documentsPath = [paths objectAtIndex:0]; // get the path to our Data/plist file NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; // set the variables to the values in the text fields self.userTitle = myTitle.text; self.userMessage = myMessage.text; NSLog(@"METHOD SAVE"); NSLog(@"Datepicker value = %@", myDate.date ); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSString *mdate = [dateFormatter stringFromDate:myDate.date]; // [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSLog(@"Converted from date picker, save into plist = %@", mdate ); self.saveDate = mdate; //self.userDate = myDate.date; // create dictionary with values in UITextFields NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: userTitle, userMessage, saveDate, nil] forKeys:[NSArray arrayWithObjects: @"Title", @"Message", @"UserDate", nil]]; NSString *error = nil; // create NSData from dictionary NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&amp;error]; // check is plistData exists if(plistData) { // write plistData to our Data.plist file [plistData writeToFile:plistPath atomically:YES]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your settings have been saved." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",nil]; [alert show]; } else { NSLog(@"Error in saveData: %@", error); //[error release]; } </code></pre> <p>viewWillAppear</p> <pre><code>- (void)viewWillAppear:(BOOL)animated { [self.navigationController setNavigationBarHidden:YES]; [super viewWillAppear:animated]; // Data.plist code // get paths from root direcory NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path NSString *documentsPath = [paths objectAtIndex:0]; // get the path to our Data/plist file NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; // check to see if Data.plist exists in documents if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { // if not in documents, get property list from main bundle plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; } // read property list into memory as an NSData object NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; NSString *errorDesc = nil; NSPropertyListFormat format; // convert static property liost into dictionary object NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&amp;format errorDescription:&amp;errorDesc]; if (!temp) { NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); } NSLog(@"Plist Title: %@", [temp objectForKey:@"Title"]); NSLog(@"Plist Message: %@", [temp objectForKey:@"Message"]); NSLog(@"Plist date: %@", [temp objectForKey:@"UserDate"]); userTitle.text = [NSString localizedStringWithFormat:@"%@", [temp objectForKey:@"Title"]]; userMessage.text = [NSString localizedStringWithFormat:@"%@", [temp objectForKey:@"Message"]]; //http://stackoverflow.com/questions/1070354/how-do-i-get-the-current-date-in-cocoa NSDate* now = [[NSDate alloc] initWithTimeIntervalSinceNow:3600]; userCoundown.text = [NSString stringWithFormat:@"%@",now]; NSString *uDate = [[NSString alloc] init]; uDate = [temp objectForKey:@"UserDate"]; NSLog(@"Plist date in string - %@", uDate); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; NSDate *ddate = [dateFormatter dateFromString:uDate]; [dateFormatter setDateFormat:@"yyyy-MM-dd H:mm:ss Z"]; NSLog(@"Format date from Plist - %@", [dateFormatter stringFromDate:ddate]); //NSLog(@"GMT format from Plist - %@", timeStamp); //http://www.epochconverter.com/# destinationDate = ddate; timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; } </code></pre> <p>updatelabel</p> <pre><code>-(void)updateLabel { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; int units = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0]; [datelabel setText:[NSString stringWithFormat:@"%d%c %d%c %d%c %d%c %d%c", [components month], 'm', [components day], 'd', [components hour], 'h', [components minute], 'm', [components second], 's']]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload