2011-04-28 75 views

回答

2

获取日期/显示日期对桌面文件:

- (IBAction)theButton:(id)sender { 
    NSString * currentDate = [[NSDate date] description]; 
    [theLabel setStringValue:currentDate]; 
    [currentDate writeToFile:@"/Users/Anne/Desktop/log.txt" atomically:YES encoding:NSUnicodeStringEncoding error:NULL]; 
} 

结果:

2011-04-28 20点18分02秒+0200

获取时间/显示文本框内的时间/写入桌面文件的时间:

- (IBAction)theButton:(id)sender { 
    NSDate * now = [NSDate date]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"hh:mm:ss"]; 
    NSString *currentTime = [formatter stringFromDate:now]; 
    [theLabel setStringValue:currentTime]; 
    [currentTime writeToFile:@"/Users/Anne/Desktop/log.txt" atomically:YES encoding:NSUnicodeStringEncoding error:NULL]; 
} 

结果:

8时十七分41秒

注意:您可以通过使用NSDateFormatter更改日期的任何格式。

+0

只需要在uilabel outlet ... – Askapps 2011-04-28 18:20:47

+0

我怎样才能得到日期呢? – Askapps 2011-04-28 18:20:55

+0

再次感谢:) – Askapps 2011-04-28 18:22:35