简体   繁体   English

如何将当前时间分成3个字符串; 小时,分钟和秒

[英]How do I split the current time into 3 strings; hours, minutes, and seconds

I'm making a clock app, and I want to display the current time, with the components (hours minutes, seconds) spaced out. 我正在制作一个时钟应用程序,我想显示当前时间,并隔开各个组件(小时,分钟,秒)。 I think that the only way to do that is to separate the time into three strings. 我认为唯一的方法就是将时间分成三个字符串。 How do I do that? 我怎么做?

- (void)updateTime {

    [updateTimer invalidate];
    updateTimer = nil;

    currentTime = [NSDate date];
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
    [timeFormatter setTimeStyle:NSDateFormatterMediumStyle];
    lblTime.text = [timeFormatter stringFromDate:currentTime];

    updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

}

A couple things here: 这里有几件事:

1 ) if you want a constantly running timer, don't invalidate it each time " updateTime " is called (and only call scheduledTimerWithTimeInterval: once... instead of at the end of each time " updateTime " is called). 1)如果您想要一个持续运行的计时器,则不要在每次调用“ updateTime ”时都将其无效(并且仅调用scheduledTimerWithTimeInterval:一次...而不是在每次调用“ updateTime ”结束时)。

2 ) it looks like you have a single label for displaying your time. 2)看起来您只有一个标签来显示您的时间。 You should make your timeFormatter an ivar (instance variable, so it's created and set only once) and then you can set the format via: 您应该将timeFormatter一个ivar(实例变量,因此只能创建并设置一次),然后可以通过以下方式设置格式:

timeFormatter.dateFormat = @"HH:mm:ss";

And you should be all set. 而且你应该准备好了。

Your new updateTime method could look like this: 您的新updateTime方法可能如下所示:

- (void)updateTime {    
    currentTime = [NSDate date];
    lblTime.text = [timeFormatter stringFromDate:currentTime];    
}

3 ) If you want three different labels, you need to declare IBOutlets for the three labels and have three different date formatters. 3)如果需要三个不同的标签,则需要为这三个标签声明IBOutlets,并且具有三个不同的日期格式化程序。 For example: 例如:

@interface YourViewController : UIViewController ()
{
    IBOutlet UILabel * hourLabel; // you need to connect these in your XIB/storyboard
    IBOutlet UILabel * minuteLabel;
    IBOutlet UILabel * secondLabel;

    NSDateFormatter * hourFormatter;
    NSDateFormatter * minuteFormatter;
    NSDateFormatter * secondFormatter;
}

Then, in your " viewDidLoad: " method, set up your formatters: 然后,在您的“ viewDidLoad: ”方法中,设置格式化程序:

hourFormatter = [[NSDateFormatter alloc] init;
hourFormatter.dateFormatter = @"HH";
minuteFormatter = [[NSDateFormatter alloc] init;
minuteFormatter.dateFormatter = @"mm";
secondFormatter = [[NSDateFormatter alloc] init;
secondFormatter.dateFormatter = @"ss";

And finally, in your " updateTime " method: 最后,在您的“ updateTime ”方法中:

- (void)updateTime {    
    currentTime = [NSDate date];
    if(hourFormatter)
    {
        if(hourLabel)
        {
            hourLabel.text = [hourFormatter stringFromDate: currentTime];
        } else {
            NSLog( @"you need to connect your hourLabel outlet in your storyboard or XIB" );
        }
    } else {
        NSLog( @"you need to allocate and init and set hourFormatter");
    }
    minuteLabel.text = [minuteFormatter stringFromDate: currentTime];
    secondLabel.text = [secondFormatter stringFromDate: currentTime];
}

If you just need a single string, you can change 如果只需要一个字符串,则可以更改

[timeFormatter setTimeStyle:NSDateFormatterMediumStyle];

to

[timeFormatter setDateFormat:@"hh:mm:ss"];

You can use to separate hh, mm, ss by using 您可以使用来分隔hh,mm,ss

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSInteger minutes = [components minute];
NSInteger seconds = [components second];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM