gpt4 book ai didi

iphone - 如何通过单击按钮更改数字时钟的时间格式?

转载 作者:行者123 更新时间:2023-12-03 21:18:25 25 4
gpt4 key购买 nike

我有一个数字时钟,我通过 0.png 到 9.png 的图像显示它。数字时钟显示正常,但问题是其时间格式为 24 小时制。

我尝试将时钟格式更改为 12 小时制,但它仍然以 24 小时制显示时钟。我怎样才能做到这一点?

这是我的代码:

NSDate *dateShow= [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setTimeStyle:NSDateFormatterNoStyle];
//[dateFormat setDateFormat:@"hh:mm"];
/
//NSString *dateString = [dateFormat stringFromDate:dateShow];


NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * components =
[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger firstHourDigit = hour/10;
NSInteger secondHourDigit = hour%10;
NSInteger firstMinuteDigit = minute/10;
NSInteger secondMinuteDigit = minute%10;

int res=0;
for(int i=0;i<4;i++)
{

if(i==0)
res = firstHourDigit;
else if(i==1)
res = secondHourDigit;
else if (i==2)
res = firstMinuteDigit;
else
res = secondMinuteDigit;

switch (res) {

case 0:
str_imageName=@"0.png";
break;
case 1:
str_imageName=@"1.png";
break;
case 2:
str_imageName=@"2.png";
break;
case 3:
str_imageName=@"3.png";
break;
case 4:
str_imageName=@"4.png";
break;
case 5:
str_imageName=@"5.png";
break;
case 6:
str_imageName=@"6.png";
break;
case 7:
str_imageName=@"7.png";
break;
case 8:
str_imageName=@"8.png";
break;
case 9:
str_imageName=@"9.png";
break;

default:
break;
}
if(i==0)
[minfirstImage setImage:[UIImage imageNamed:str_imageName]];
else if(i==1)
[minsecondImage setImage:[UIImage imageNamed:str_imageName]];
else if (i==2)
[secfirstImage setImage:[UIImage imageNamed:str_imageName]];
else
[secSecondImage setImage:[UIImage imageNamed:str_imageName]];



}

最佳答案

对您的时间使用模数...

value % 12

所以 9 % 12 = 9, 14 % 12 = 2.... 等等

关于iphone - 如何通过单击按钮更改数字时钟的时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com