【问题标题】:Local time on the iPhoneiPhone 上的当地时间
【发布时间】:2011-09-25 22:25:32
【问题描述】:

我需要显示准确的本地时间,并且时间应该运行。

如果可能,请提供示例代码或解释。

谢谢。

【问题讨论】:

    标签: iphone objective-c time nstimer


    【解决方案1】:

    这将返回带有时间的当前日期

    [NSDate date];
    

    现在您可以使用 NStimer 每隔一秒获取时间并显示它。

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(displayTime) userInfo:nil repeats:NO];
    //Time Interval is one second
    -(void) displayTime
    {
    
        NSDate *currentDate = [NSDate date];
        NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
        timeFormatter.dateFormat = @"HH:mm:ss a";
        NSString *dateString = [timeFormatter stringFromDate: currentDate];
        yourlabel.text = dateString;
        [timeFormatter release]
    }
    

    【讨论】:

    • 感谢它运行良好的代码,我还有一个问题如何显示上午/下午的时间。请帮忙!
    • timeFormatter.dateFormat = @"HH:mm:ss a";
    猜你喜欢
    • 2010-10-08
    • 2011-12-10
    • 1970-01-01
    • 2018-01-14
    • 2012-11-29
    • 2013-01-06
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多