IOSでシステム時刻を取得するサンプルコード

方法1.

システム時刻を取得
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"HH:mm"];
NSString * locationString=[dateformatter stringFromDate:senddate];
//[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
//NSString * morelocationString=[dateformatter stringFromDate:senddate];

//システム日付を取得
NSCalendar * cal=[NSCalendar currentCalendar];
NSUInteger unitFlags=NSDayCalendarUnit
方法2
NSYearCalendarUnit;
NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
NSInteger year=[conponent year];
NSInteger month=[conponent month];
NSInteger day=[conponent day];
NSString * nsDateString= [NSString stringWithFormat:@"%4d年%2d月%2d日",year,month,day];
[dateformatter release];

方法3
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *currentTime = [formatter stringFromDate:[NSDate date]];

IOS

Posted by arkgame