LoginSignup
0
0

More than 5 years have passed since last update.

Objective-C 年月日時分からNSDateを初期化

Last updated at Posted at 2017-11-22

テストデータ作成時によく使いそうなので残しておく

Objective-C
- (NSDate*)createDate:(int)year :(int)month :(int)day :(int)hour :(int)min
{
    NSDateComponents *com = [[NSDateComponents alloc] init];
    [com setYear:year];
    [com setMonth:month];
    [com setDay:day];
    [com setHour:hour];
    [com setMinute:min];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calender dateFromComponents:com];
}

呼び出し元

Objective-C
NSDate *date = [self createDate:2017 :4 :1 :12 :15];
NSLog(@"%@", date);

ログ

Log
Sat Apr  1 12:15:00 2017

端末の設定で、
使用言語を「日本語」→「English(U.S.)」に変更しても、
同じ結果が出力されたので問題はなさそう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0