我正在构建复杂的Xcode应用程序,并且我被要求有一个开始时间,并且停止时间字段,计算到十进制字段。
现在时间必须是24小时格式,只能是HHmm格式,而不是HH:mm!
当数字在字段中开始/停止时有一个从右边开始的计算,计算左边两个char $将是分钟,第三个和第四个char $将是小时。然后转换为小数0.00。
我从哪里开始?
Stack Overflow问题 Time in HHMM format 适用于Java。 Xcode怎么样?
答案 0 :(得分:0)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HHmm"];
NSString *timeString = [formatter stringFromDate:[NSDate date]];
答案 1 :(得分:0)
// Sample code
double duration = 2.75;
double decimal = floor(duration) - duration; // decimal = .75 (floor will round down)
double minutes = .75 * 60; // 60 minutes per hour, multiply to convert from decimal to minutes
答案 2 :(得分:0)
小数时间
输入数字到参考时间,但不要在24小时或更长时间内输入十进制数字。唯一的计算是将一串数字拆分为两个字符串,并以十进制格式计算开始和结束。
// Strings converted to integers, and float integers back into strings.
// Time entered in is ONLY 24 hour with 00,15,30,45 minutes.
@synthesize labelTotal;
@synthesize labelStart;
@synthesize labelStop;
@synthesize remarkLabel;
NSString *START = [self.startTextField text];
NSString *END = [self.stopTextField text];
remarkLabel.text = @"";
//Take START convert to Decimal
NSLog(@"startString: %@", START);
//Convert nsstring (24 Hour) to decimal
int a = [self.startTextField.text intValue];
int startHH = a / 100;
int startmm = a % 100;
NSLog(@"startHH: %d", startHH);
NSLog(@"startmm: %d", startmm);
//Define startmm
if (startmm == 15)
{
NSInteger mmTime = 25;
NSLog(@"Start Hour is: %d", startHH);
NSLog(@"Start Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStart1a = [NSString stringWithFormat:@"%d", startHH];
NSString *newStart1b = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStart1a = [NSString stringWithFormat:@"%@.%@", newStart1a, newStart1b];
NSLog(@"Decimal Start Time is: %@", recStart1a);
labelStart.text = recStart1a;
}
else
if (startmm == 30)
{
int mmTime = 50;
NSLog(@"Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStart1d = [NSString stringWithFormat:@"%d", startHH];
NSString *newStart1e = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStart1b = [NSString stringWithFormat:@"%@.%@", newStart1d, newStart1e];
NSLog(@"Decimal Start Time is: %@", recStart1b);
labelStart.text = recStart1b;
}
else
if (startmm == 45)
{
int mmTime = 75;
NSLog(@"Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStart1g = [NSString stringWithFormat:@"%d", startHH];
NSString *newStart1h = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStart1c = [NSString stringWithFormat:@"%@.%@", newStart1g, newStart1h];
NSLog(@"Decimal Start Time is: %@", recStart1c);
labelStart.text = recStart1c;
}
else
if (startmm == 00)
{
NSString *newStart1g = [NSString stringWithFormat:@"%d", startHH];
NSString *recStart1c = [NSString stringWithFormat:@"%@.00", newStart1g];
NSLog(@"Decimal Start Time is: %@", recStart1c);
labelStart.text = recStart1c;
}
//Take END convert to Decimal
NSLog(@"stopString: %@", END);
int b = [self.stopTextField.text intValue];
int stopHH = b / 100;
int stopmm = b % 100;
NSLog(@"stopHH: %d", stopHH);
NSLog(@"stopmm: %d", stopmm);
//Define stopmm
if (stopmm == 15)
{
NSInteger mmTime = 25;
NSLog(@"Start Hour is: %d", stopHH);
NSLog(@"Start Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStop1a = [NSString stringWithFormat:@"%d", stopHH];
NSString *newStop1b = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStop1a = [NSString stringWithFormat:@"%@.%@", newStop1a, newStop1b];
NSLog(@"Decimal Start Time is: %@", recStop1a);
labelStop.text = recStop1a;
}
else
if (stopmm == 30)
{ int mmTime = 50;
NSLog(@"Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStop1d = [NSString stringWithFormat:@"%d", stopHH];
NSString *newStop1e = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStop1b = [NSString stringWithFormat:@"%@.%@", newStop1d, newStop1e];
NSLog(@"Decimal Start Time is: %@", recStop1b);
labelStop.text = recStop1b;
}
else if (stopmm == 45)
{
int mmTime = 75;
NSLog(@"Minutes is: %ld", (long)mmTime);
//Combine startHH.mmTime
NSString *newStop1g = [NSString stringWithFormat:@"%d", stopHH];
NSString *newStop1h = [NSString stringWithFormat:@"%ld", (long)mmTime];
NSString *recStop1c = [NSString stringWithFormat:@"%@.%@", newStop1g, newStop1h];
NSLog(@"Decimal Start Time is: %@", recStop1c);
labelStop.text = recStop1c;
}
else
if (stopmm == 00)
{
NSString *newStop1g = [NSString stringWithFormat:@"%d", stopHH];
NSString *recStop1c = [NSString stringWithFormat:@"%@.00", newStop1g];
NSLog(@"Decimal Start Time is: %@", recStop1c);
labelStop.text = recStop1c;
}
//Convert back into decimal
float start1 = [self.labelStart.text floatValue];
float stop1 = [self.labelStop.text floatValue];
NSLog(@"Start Start: %f", start1);
NSLog(@"Stop Stop: %f", stop1);
//Subtact END from START in Decimal
float c = stop1 - start1;
//Show TOTAL in Decimal
NSString *total1 = [NSString stringWithFormat:@"%g", c];
if ([total1 isEqual:@"0"])
{
labelTotal.text = @"";
NSLog(@"BLANK");
}
else
{
labelTotal.text = total1;
NSLog(@"Total: %@", total1);
}
//You can add additional if statements below for more conditions needed
}