如何通过单击按钮更改数字时钟的时间格式? [英] How to change the time format of a digital clock with a button click?

查看:52
本文介绍了如何通过单击按钮更改数字时钟的时间格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字时钟,我正在显示从0.png到9.png的图像.数字时钟显示正确,但问题在于其时间格式为24小时.

I have a digital clock which I am displaying through images from 0.png to 9.png. The digital clock is getting displayed properly but the problem is its time format is in 24 hour.

我试图将时钟的格式更改为12h,但是它仍然在24h中显示时钟.我该怎么办?

I have tried to change the format of the clock to 12h but it still displays the clock in 24h. How can I do this?

这是我的代码:

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]];



        }

推荐答案

在您的时间上使用模数...

Use a modulo on your hour...

value % 12

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

So 9 % 12 = 9, 14 % 12 = 2.... etc

这篇关于如何通过单击按钮更改数字时钟的时间格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆