PDA

View Full Version : NSDateFormatter tweek




Hig4ness
Sep 2, 2012, 08:23 AM
Hi guys!

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


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:
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



CodeBreaker
Sep 2, 2012, 08:53 AM
Umm, instead of [NSDate date] use

[NSDate dateWithTimeIntervalSinceNow:(-10 * 60)]

And after that just set your date formatter's style to HH:mm.

Hig4ness
Sep 2, 2012, 09:23 AM
Codebreaker: I think i forgot to add, ive just been using Xcode for 10 hours ;)

anyhow, i did this

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

PhoneyDeveloper
Sep 2, 2012, 11:41 AM
The correct way to do math on dates is by using NSDateComponents.

Read the Date and Time Programming Guide.

CodeBreaker
Sep 2, 2012, 02:27 PM
Codebreaker: I think i forgot to add, ive just been using Xcode for 10 hours ;)

anyhow, i did this

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:


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:(-10 * 60)];
[dateFormatter setDateFormat:@"HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];

lbl.text =dateString;

Hig4ness
Sep 2, 2012, 03:43 PM
Thanks CodeBreaker!

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


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

Duncan C
Sep 3, 2012, 08:58 AM
Thanks CodeBreaker!

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


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.