NSDateFormatter tweek

Discussion in 'iPhone/iPad Programming' started by Hig4ness, Sep 2, 2012.

  1. Hig4ness, Sep 2, 2012
    Last edited by a moderator: Sep 2, 2012

    macrumors newbie

    Joined:
    Sep 2, 2012
    #1
    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
     
  2. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #2
    Umm, instead of [NSDate date] use
    Code:
    [NSDate dateWithTimeIntervalSinceNow:(-10 * 60)]
    
    And after that just set your date formatter's style to HH:mm.
     
  3. thread starter macrumors newbie

    Joined:
    Sep 2, 2012
    #3
    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
     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    The correct way to do math on dates is by using NSDateComponents.

    Read the Date and Time Programming Guide.
     
  5. macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #5
    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;
    
     
  6. thread starter macrumors newbie

    Joined:
    Sep 2, 2012
    #6
    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];
    
     
  7. macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    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.
     

Share This Page