OCdemo - 07 NSDate 날짜 설정 형식 NSDateFormatter

//
//  main.m
//  OCdemo-07
//
//  Created by lanou3g on 15/10/15.
//  Copyright (c) 2015  Object. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
     
        
#pragma mark NSDate
        
        //       
        NSDate *date = [NSDate date];
        NSLog(@"%@",date);
        
        //       
        NSDate *eightDate = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60];
        NSLog(@"     :%@",eightDate);
        //       
        NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60 + 24 * 60 * 60];
        NSLog(@"    :%@",tomorrowDate);
        //      
        NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60 - 24 * 60 * 60];
        NSLog(@"    :%@",yesterdayDate);
        //         
        NSTimeInterval interval = [yesterdayDate timeIntervalSinceDate:tomorrowDate];
        NSLog(@"%f",interval);
        
        //        1970 1 1     
        NSTimeInterval time1970 = [date timeIntervalSince1970];
        NSLog(@"time1970 = %f",time1970);
        
        //                ,     60  ,  “  ”,   60  3600  ,  “xx   ”,  3600  ,3600*24  ,  “xx   ”
        NSDate *nowDate = [NSDate date];
        NSDate *oneDate = [NSDate dateWithTimeIntervalSinceNow:-4839];
        
        //         
        NSTimeInterval subTime = [nowDate timeIntervalSinceDate:oneDate];
        if (subTime < 60) {
            NSLog(@"  ");
        } else if (subTime >= 60 && subTime <= 3600 ){
            NSLog(@"%d  ",(int)subTime / 60);
        } else if (subTime > 3600 && subTime < 24 * 3600){
            NSLog(@"%d   ",(int)subTime / 3600);
        }
      
        
#pragma mark           NSDateFormatter
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //      
        [dateFormatter setDateFormat:@"yyyy MM dd  HH mm ss "];
        NSDate *date = [NSDate date];
        NSString *dateStr = [dateFormatter stringFromDate:date];
        NSLog(@"%@",dateStr);
        
        //
        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
        //      
        [myDateFormatter setDateFormat:@"GGGG yyyy MM dd  HH mm ss  aaaa QQQQ EEEE"];
        
        NSLog(@"    : %@",[myDateFormatter stringFromDate:date]);
        //    :    2015 10 15  11 00 53             
        
        
//        //         NSDate  
//        NSString *dateString = @"2015 9 14  8 30 59 ";
//        NSDateFormatter *setDateFormatter = [[NSDateFormatter alloc] init];
//        [setDateFormatter setDateFormat:@"yyyy MM dd  HH mm ss "];
//        NSDate *dateFromString = [setDateFormatter dateFromString:dateString];
//        NSLog(@"%@",dateFromString);
        
        //    @“2014 05 01  10 23 18 ”   NSDate  。
        NSString *string = @"2014 05 01  10 23 18 ";
        NSDateFormatter *setDateFormatter = [[NSDateFormatter alloc] init];
        [setDateFormatter setDateFormat:@"yyyy MM dd  HH mm ss "];
        NSDate *dateFromString = [setDateFormatter dateFromString:string];
        NSLog(@"%@",dateFromString);
        
       
    

        
    }
    return 0;
}

좋은 웹페이지 즐겨찾기