Query for date time calculation.

Discussion in 'Mac Programming' started by newformac, May 27, 2011.

  1. newformac macrumors regular

    May 24, 2011
    Hi all,

    i m creating a file that contains date time and current locale,
    and after creating the file when application start read the file and calculate the time difference from file time to currently running time.

    //Install Date/Time Reference File
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	NSString *installDateFile = @"~/Desktop/setlDateFile.txt";
    	installDateFile = [installDateFile stringByExpandingTildeInPath];
    	if ([fileManager fileExistsAtPath: installDateFile] == NO)
    		NSString *installDate = [[NSDate date] descriptionWithCalendarFormat:nil timeZone:nil locale:nil];
    		[installDate writeToFile:installDateFile atomically:YES encoding:NSUnicodeStringEncoding error:NULL];
    double secondsUsed = [[NSDate date] timeIntervalSinceDate:[NSDate dateWithString:[NSString stringWithContentsOfFile:[@"~/Desktop/setlDateFile.txt" stringByExpandingTildeInPath]]]];

    now i want to write only date value in file and calculate the days difference from file date to current date.

    please help me how can i do this.

    thanks in advance.
  2. Sydde macrumors 68020


    Aug 17, 2009
    You should look at NSKeyedArchiver (for writing to the file) and NSKeyedUnarchiver (for reading back). You can encode/decode the actual NSDate object itself instead of converting it to a string, that way the user will have a much harder time altering its value and you can easily localize it after decoding.
  3. subsonix macrumors 68040

    Feb 2, 2008
    Doesn't files default to adding creation date/modification date on HFS? You should be able to read these with NSFileManager AFAIK.
  4. ehoui macrumors regular

    Jan 27, 2011
    The OP needs gentle nudging from us to learn to use the developer docs for most of his/her questions.
  5. newformac thread starter macrumors regular

    May 24, 2011

Share This Page