2015-01-06 18 views
9

誰でも私に日付を取得する方法を教えていただけますか? :)iOS 8 - 現在の日付をDD/MM/YYYYとして取得

おかげで... 1週間でというように、S

をそして、私は明日の日付を取得することができる方法があります:

NSDateComponents *components = [[NSCalendarcurrentCalendar] component:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYearfromDate:[NSDatedate]]; 

NSString *day = [components day]; 
NSString *week = [components month]; 
NSString *year = [components year]; 

NSString *date = [NSString stringWithFormat:@"%@.%@.%@",day,week,year]; 

^^私のコードは動作しません

+2

Googleの "NSDateFormatter" のように取得することができます。 –

+2

"not working"を定義します。 – rmaddy

+0

この質問はどのようにトピックから外れていますか?誰にも本やツール、ソフトウェアライブラリ、チュートリアル、その他のオフサイトリソースの推薦や検索を依頼するものではありません。 – Suragch

答えて

22

インク あなたはcomponents方法でNSCalendarを使用して数値のコンポーネントを取得し、あなたのコードのバリエーションを使用することができます。

または、より良い、あなたはNSDateFormatterを使用することができます。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
formatter.dateFormat = @"d.M.yyyy"; 
NSString *string = [formatter stringFromDate:[NSDate date]]; 
0

異なるコンポーネント単位を設定し、これを試してみてください。このリットルを使用し、

NSDate *today = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents * components = 
         [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:today]; 
    NSInteger day = [components day]; 
    NSInteger month = [components month]; 
    NSInteger year = [components year]; 

あなたの希望は、他の日を取得する場合それはcomponents、ないcomponentだ、

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; 

NSInteger day = [components day]; 
NSInteger month = [components month]; 
NSInteger year = [components year]; 

NSString *string = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)day, (long)month, (long)year]; 

注:calendar get other day

10
NSDate *todayDate = [NSDate date]; //Get todays date 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date. 
    [dateFormatter setDateFormat:@"dd-MM-yyyy"]; //Here we can set the format which we need 
    NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// Here convert date in NSString 
NSLog("Today formatted date is %@",convertedDateString); 

0

あなたはこの

let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "dd/MM/yyyy" 
let dateString = dateFormatter.string(from:Date()) 
print(dateString) 
関連する問題