Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hig4ness

macrumors newbie
Original poster
Sep 2, 2012
3
0
Hi guys!

Im doing a little app and have stuck. Im using NSDateFormatter to print out the current time, like this:

Code:
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"HH"];
    NSDate *currentDate1 = [NSDate date];
    NSString *hour = [dateFormatter1 stringFromDate:currentDate1];
    
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"mm"];
    NSDate *currentDate2 = [NSDate date];
    NSString *mins = [dateFormatter2 stringFromDate:currentDate2];

By putting a sting into an action i can get the current time:
Code:
tabb1lbl2.text=[NSString stringWithFormat:@"%@%@%@",hour,@":",mins];

Not to my problem.. this code gives me the current time, but i would like to withdraw 10 minutes from the current time. So if the time is 15:30 i would like to print out 15:20.

Is there any solution to my problem?

Sincerely,
Jim
 
Last edited by a moderator:
Codebreaker: I think i forgot to add, ive just been using Xcode for 10 hours ;)

anyhow, i did this
Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:(-10 * 60)];
    [dateFormatter setDateFormat:@"HH:mm"];
    
    
    lbl.text =[NSString stringWithFormat:@"%@",currentDate];

and the output got: 2012-09-02 14:12:03 +0000

My current system time is 16:22

I simply want the label to be 16:12
 
The correct way to do math on dates is by using NSDateComponents.

Read the Date and Time Programming Guide.
 
Codebreaker: I think i forgot to add, ive just been using Xcode for 10 hours ;)

anyhow, i did this
Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:(-10 * 60)];
    [dateFormatter setDateFormat:@"HH:mm"];
    
    
    lbl.text =[NSString stringWithFormat:@"%@",currentDate];

and the output got: 2012-09-02 14:12:03 +0000

My current system time is 16:22

I simply want the label to be 16:12

You are not converting your date to a string, like you did before.

So it should be like this:

Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:(-10 * 60)];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    lbl.text =dateString;
 
Thanks CodeBreaker!

heres how it looks now and work like a charm..

Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSDate *currentDate = [NSDate date];
    NSDate *newDate = [currentDate dateByAddingTimeInterval:-600];
    NSString *time = [dateFormatter stringFromDate:newDate];
 
Thanks CodeBreaker!

heres how it looks now and work like a charm..

Code:
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSDate *currentDate = [NSDate date];
    NSDate *newDate = [currentDate dateByAddingTimeInterval:-600];
    NSString *time = [dateFormatter stringFromDate:newDate];

I agree with Phoney. The cleaner way to do what you're talking about is to create an NSCalendar object and do math using NSDateComponents. If all you need for output is hours:minutes, you might want to generate that yourself using date components as well.

Date formatters are a tad confusing, in that the format "HH:mm" does not guarantee that you will get 2 characters of hour, a colon, and 2 characters of date. Date formatters vary their output based on the user's locale and the date formatting conventions appropriate to that locale. If your goal is to present date strings in the user's native format, they're great. If you want consistent date strings that you are going to use programmatically, using date components and building your strings yourself is the way to go.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.