2011-04-28 46 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