NSDate를 NSString으로 변환 할 NSDate에 NSString그렇게 만 해

어떻게 변환 할 NSDateNSString그렇게 만 해 @ “YYYY” 문자열 출력 형식입니다?



답변

어때요?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];

//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];

//unless ARC is active
[formatter release];

스위프트 4.2 :

func stringFromDate(_ date: Date) -> String {
    let formatter = DateFormatter()
    formatter.dateFormat = "dd MMM yyyy HH:mm" //yyyy
    return formatter.string(from: date)
}


답변

우리 모두가 이것을 놓친 방법을 모르겠습니다 : localizedStringFromDate : dateStyle : timeStyle :

NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] 
                                                      dateStyle:NSDateFormatterShortStyle 
                                                      timeStyle:NSDateFormatterFullStyle];
NSLog(@"%@",dateString);

’13 / 06 / 12 00:22:39 GMT + 03 : 00 ‘출력


답변

시간과 함께 년, 월, 일을 포함한 일반적인 포맷터를 제공하여 더 많은 가치를 창출하기를 바랍니다. 이 포맷터를 1 년 이상 사용할 수 있습니다

[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"]; 


답변

NSDate웹 에는 많은 도우미가 있습니다.

https://github.com/billymeltdown/nsdate-helper/

아래의 추가 정보 추출 :

  NSString *displayString = [NSDate stringForDisplayFromDate:date];

다음과 같은 종류의 출력이 생성됩니다.

3:42 AM  if the date is after midnight today
Tuesday  if the date is within the last seven days
Mar 1  if the date is within the current calendar year
Mar 1, 2008  else ;-)


답변

스위프트에서 :

var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy"
var dateString = formatter.stringFromDate(YourNSDateInstanceHERE)


답변

  NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
  [dateformate setDateFormat:@"yyyy"]; // Date formater
  NSString *date = [dateformate stringFromDate:[NSDate date]]; // Convert date to string
  NSLog(@"date :%@",date);


답변

NSDate -descriptionWithCalendarFormat:timeZone:locale:사용할 수 없는 경우 (iPhone / Cocoa Touch에 포함되어 있다고 생각하지 않습니다) strftime과 monkey를 일부 C 스타일 문자열과 함께 사용해야합니다. 를 NSDate사용하여 UNIX 타임 스탬프를 얻을 수 있습니다 NSDate -timeIntervalSince1970.