将多分钟数字(超过 2)分钟转换为 NSDate
Converting multiple minutes digits (more than 2) minutes to NSDate
我正在尝试解析由多个分钟数组成的时钟字符串,并使用 NSDateFormatter
.
将其转换为 NSDate
我试图解析的字符串示例是 @"1234:56"
,其中 1234 是时钟的分钟,56 是秒。我尝试使用 @"mmmm:ss"
之类的格式,但它返回了 nil。
如果可能的话,谁能帮我解决这个问题?
谢谢
NSDateFormatter
只适用于合法的日期格式,没有'mmmm'。你应该自己获取日期:
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.
试试这个。
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;
如果您想将其转换为 HH:mm:ss,那么我的方法类似于:
// 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];
像这样
我不确定你想做什么,但我建议你这样做:
NSArray *timeArray = [@"1234:56" componentsSeparatedByString:@":"];
NSUInteger minutes = [[timeArray firstObject] integerValue];
NSUInteger seconds = [[timeArray lastObject] integerValue];
NSTimeInterval totalSeconds = minutes*60+seconds;
然后您应该创建一个新的日期对象并使用它。
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
我正在尝试解析由多个分钟数组成的时钟字符串,并使用 NSDateFormatter
.
NSDate
我试图解析的字符串示例是 @"1234:56"
,其中 1234 是时钟的分钟,56 是秒。我尝试使用 @"mmmm:ss"
之类的格式,但它返回了 nil。
如果可能的话,谁能帮我解决这个问题?
谢谢
NSDateFormatter
只适用于合法的日期格式,没有'mmmm'。你应该自己获取日期:
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.
试试这个。
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;
如果您想将其转换为 HH:mm:ss,那么我的方法类似于:
// 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];
像这样
我不确定你想做什么,但我建议你这样做:
NSArray *timeArray = [@"1234:56" componentsSeparatedByString:@":"];
NSUInteger minutes = [[timeArray firstObject] integerValue];
NSUInteger seconds = [[timeArray lastObject] integerValue];
NSTimeInterval totalSeconds = minutes*60+seconds;
然后您应该创建一个新的日期对象并使用它。
NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:totalSeconds];