简体   繁体   English

iOS中的当前和更新时间?

[英]Current and Updating time in iOS?

i'm developing a tool for car driving which saves GPS Data, Accelerometer Data and Time for later use. 我正在开发一种汽车驾驶工具,可以保存GPS数据,加速度计数据和时间供以后使用。

Acceleration is the least Priority, though. 不过,加速度是最不重要的。

All i need is a little code example so i can get the current system time printed as an updating clock in Xcode because i want it to save my data as fast and as often as possible :> 我需要的只是一个小代码示例,因此我可以将当​​前系统时间打印为Xcode中的更新时钟,因为我希望它尽可能快地保存我的数据:>

Thanks in advance for any answers :) 在此先感谢任何答案:)

h4wkeye h4wkeye

To get the current system time use 要使用当前系统时间

NSDate* currentDate = [NSDate date];

and To get the string representation of NSDate 并获取NSDate的字符串表示形式

- (NSString *)description

Then Use below 然后在下面使用

NSString* dateInString = [currentDate description];

Like to share this simple code example. 喜欢分享这个简单的代码示例。 Just put this any where in your myClass code. 只需将其放在myClass代码中的任何位置即可。 You May find more formatting examples > Format String for the iPhone NSDateFormatter. 您可以找到更多格式示例> iPhone NSDateFormatter的格式字符串。

// Get current date time

NSDate *currentDateTime = [NSDate date];

// Instantiate a NSDateFormatter

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// Set the dateFormatter format

//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// or this format to show day of the week Sat,11-12-2011 23:27:09

[dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];

// Get the date time in NSString

NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];

NSLog(@"%@", dateInStringFormated);

// Release the dateFormatter

[dateFormatter release]; 

Use this code in viewDidLoad or viewWillAppear 在viewDidLoad或viewWillAppear中使用此代码

-(void)callUpdate
{
NSTimer *timer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:0.1 target:self     selector:@selector(updateTimer) userInfo:nil repeats:YES];

}
-(void)updateTimer
{
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];

[dateformatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];

NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate date]];
label.text = dateInStringFormated;

}

or else just alloc the timer in the viewDidLoad or viewWillAppear and when you need to stop the timer use 或者只是在viewDidLoad或viewWillAppear中分配计时器,当你需要停止使用计时器时

[timer invalidate];

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

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