-4

How should I import the current time into my app and use it to do some calculation?

What my app do is that it use the current time and do some calculation and then give me some percentage.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jeff
  • 1

2 Answers2

1

How are you wanting to get the time (in milliseconds?)

There's a few questions on here already - like this one, which should help point you in the right direction.

From the Apple Docs for NSDate

Creates and returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.

[[NSDate date] timeIntervalSince1970];

Community
  • 1
  • 1
SilkyPantsDan
  • 561
  • 4
  • 5
0
// use this method to get current time as per your iPhone's time format
    +(NSString *)getCurrTime
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setLocale:[NSLocale currentLocale]];
        [formatter setDateStyle:NSDateFormatterNoStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        NSString *dateString = [formatter stringFromDate:[NSDate date]];
        NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
        NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];

        BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
        if (is24h == YES) {
            [formatter setDateFormat:@"HH:mm"];
        }
        else
        {
            [formatter setDateFormat:@"hh:mm a"];
        }
        dateString =[formatter stringFromDate:[NSDate date]];
        NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));
        return dateString;
    }
Vishal Gandhi
  • 256
  • 2
  • 3