2013-03-15 2 views
3

텍스트 문자열을 데이터 파일에 쓰는 방법을 만들었습니다. 시스템 날짜와 시간을 어떻게 가져 와서 텍스트 파일로 보낼 수 있는지 궁금합니다. 사용 NSDate시스템 날짜 및 타임 스탬프 받기

-(void) writeToTextFile{ 

//get the documents dir 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

//file name to write the data to using the documents directory: 
NSString *fileName = [NSString stringWithFormat:@"%@/movbandlog.txt", 
         documentsDirectory]; 
//create content 
NSString *content = @"Data received from device\n \n \n \n \nData sent to Portal\n \n \n \n \nData received to Portal"; 

//save to documents directory 
[content writeToFile:fileName 
      atomically:NO 
      encoding:NSStringEncodingConversionAllowLossy 
       error:nil]; 

} 

답변

9

순수 코코아

선택적 NSDateFormatter를 포맷 할 : 다음은 방법입니다

NSDate *currentDate = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"]; 
NSString *dateString = [dateFormatter stringFromDate:currentDate]; 
// NSLog(@"%@",dateString); 

// Adding your dateString to your content string 
NSString *content = [NSString stringWithFormat:@"Data received from device\n \n \n \n \nData sent to Portal\n \n \n \n \nData received to Portal\n \n \n \n \nData received at %@", dateString]; 

을 유닉스

기능 사용 또한 using unix functions for unlocalized을 고려해 볼 수 있습니다 날짜 (timest) A) 물론

struct tm sometime; 
const char *formatString = "%Y-%m-%d %H:%M:%S %z"; 
(void) strptime_l("2005-07-01 12:00:00 -0700", formatString, &sometime, NULL); 
NSLog(@"NSDate is %@", [NSDate dateWithTimeIntervalSince1970: mktime(&sometime)]); 
// Output: NSDate is 2005-07-01 12:00:00 -0700 
+0

, 일반적인주의 사항 - 당신이 –

+0

가 어떻게이 추가 가겠어요 등 로케일 기능에 의해 비트를 얻을하지 않으려는 경우가 로케일 설정, 중요 경우 시간대를 설정 내 NSString * 내용? 내 텍스트 파일 맨 위에 나와있는 날짜와 시간이 필요합니다. – TWcode

+0

답변을 업데이트했습니다 –