CRoig
CRoig

Reputation: 693

Converting multiple minutes digits (more than 2) minutes to NSDate

I am trying to parse a clock string composed by a multiple number of minutes and convert it to a NSDate using a NSDateFormatter.

An example of the string I am trying to parse is @"1234:56", where 1234 are the minutes of the clock and 56 the seconds. I tried to use a format such as @"mmmm:ss" but it returned nil.

In case it is possible, can anyone help me with this?

Thanks

Upvotes: 0

Views: 94

Answers (4)

fjtrujy
fjtrujy

Reputation: 129

I'm not sure about the thing that you want to do, but I suggest do something like that:

NSArray *timeArray = [@"1234:56" componentsSeparatedByString:@":"];
NSUInteger minutes = [[timeArray firstObject] integerValue];
NSUInteger seconds = [[timeArray lastObject] integerValue];

NSTimeInterval totalSeconds = minutes*60+seconds;

And then you should create a new date object and work with this.

NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:totalSeconds];

Upvotes: 1

Joshua
Joshua

Reputation: 2451

If you want to convert that to HH:mm:ss then my approach will be something like:

// assuming you wouldn't have any hours in the string and format will always be minutes:seconds 

 NSArray *timeArray = [@"1234:56" componentsSeparatedByString:@":"]; 

 //array's firstObject will be the "minutes/Hours"
 NSString *minutesAndHours = [timeArray firstObject];
 int hours = [minutesAndHours intValue]/60;
 int minutes = [minutesAndHours intValue]%60;
 int seconds = [timeArray lastObject];

//create the format

NSString *time = [NSString stringWithFormat:@"%d:%d:%d",hours,minutes,seconds];

//then use the time format
NSString *time = [NSString stringWithFormat:@"%d:%d:%d",hours,minutes,seconds];
NSDateFormatter *format = [NSDateFormatter new];
[format setDateFormat:@"HH:mm:ss"];
NSDate *date = [format dateFromString:time];

something like this

Upvotes: 0

iDeveloper
iDeveloper

Reputation: 941

Try this.

NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
self.timerLabel.text = timeString;

Upvotes: 0

Lumialxk
Lumialxk

Reputation: 6369

NSDateFormatter only works with legal date format and there is no 'mmmm'.You should get date by yourself:

NSString *str = @"234:56";
NSArray<NSString *> *components = [str componentsSeparatedByString:@":"];
NSInteger minute = 0;
NSInteger second = 0;
switch (components.count) {
    case 1:
        second = components[0].integerValue;
        break;
    case 2:
        second = components[0].integerValue;
        minute = components[1].integerValue;
        break;

    default:
        break;
}
// then convert to hours.

Upvotes: 1

Related Questions