문제

기본적으로 현재 날짜와 시간을 따로 가져와야합니다.

2009-04-26 
11:06:54

같은 주제에 대한 다른 질문에서 아래 코드는 생성합니다.

now:        |2009-06-01 23:18:23 +0100| 
dateString: |Jun 01, 2009 23:18| 
parsed:     |2009-06-01 23:18:00 +0100|

이것은 거의 내가 찾고있는 것이지만, 낮과 시간을 분리하고 싶습니다.

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];

NSDate *now = [[NSDate alloc] init];

NSString *dateString = [format stringFromDate:now];

NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"MMM dd, yyyy"];

NSDate *parsed = [inFormat dateFromString:dateString];

NSLog(@"\n"
"now:        |%@| \n"
"dateString: |%@| \n"
"parsed:     |%@|", now, dateString, parsed);
도움이 되었습니까?

해결책

iPhone 형식 문자열이 있습니다 유니 코드 형식. 링크 뒤에는 위의 모든 글자가 무엇을 의미하는지 설명하는 테이블이 있으므로 직접 만들 수 있습니다.

그리고 물론 데이트 포지터링을 완료하는 것을 잊지 마십시오. 위의 코드가 누출됩니다 format, now, 그리고 inFormat.

다른 팁

이것이 내가 사용한 것입니다.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"\n"
      "theDate: |%@| \n"
      "theTime: |%@| \n"
      , theDate, theTime);

[dateFormat release];
[timeFormat release];
[now release];

이 방법을 사용할 수 있습니다. 날짜를 전달할 수 있습니다.

-(NSString *)getDateFromString:(NSString *)string
{

    NSString * dateString = [NSString stringWithFormat: @"%@",string];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"your current date format"];
    NSDate* myDate = [dateFormatter dateFromString:dateString];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"your desired format"];
    NSString *stringFromDate = [formatter stringFromDate:myDate];

    NSLog(@"%@", stringFromDate);
    return stringFromDate;
}
    NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
    [dateformat setDateFormat:@"Your Date Format"];

반환으로 형식을 설정합니다 ....

yyyy-MM-dd 반품 2015-12-17 데이트

yyyy-MMM-dd 반품 2015-Dec-17 데이트

yy-MM-dd 반품 15-12-17 데이트

dd-MM-yy 반품 17-12-15 데이트

dd-MM-yyyy 반품 17-12-2015 데이트

yyyy-MMM-dd HH:mm:ss 반품 2015-Dec-17 08:07:13 날짜와 시간

yyyy-MMM-dd HH:mm 반품 2015-Dec-17 08:07 날짜와 시간

자세한 내용은 데이터 및 시간 형식을 보려면 지금 클릭하십시오.

고맙습니다.....

새로운 것은 없지만 여전히 내 방법을 공유하고 싶어합니다.

+(NSString*) getDateStringFromSrcFormat:(NSString *) srcFormat destFormat:(NSString *)
destFormat scrString:(NSString *) srcString
{
    NSString *dateString = srcString;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //[dateFormatter setDateFormat:@"MM-dd-yyyy"];
    [dateFormatter setDateFormat:srcFormat];
    NSDate *date = [dateFormatter dateFromString:dateString];

    // Convert date object into desired format
    //[dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [dateFormatter setDateFormat:destFormat];
    NSString *newDateString = [dateFormatter stringFromDate:date];
    return newDateString;
}

Swift를 위해

var dateString:String = "2014-05-20";
var dateFmt = NSDateFormatter()
// the format you want
dateFmt.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt.dateFromString(dateString)!;

스위프트 3

extension Date {

    func toString(template: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current)
        return formatter.string(from: self)
    }

}

용법

let now = Date()
let nowStr0 = now.toString(template: "EEEEdMMM") // Tuesday, May 9
let nowStr1 = now.toString(template: "yyyy-MM-dd") // 2017-05-09
let nowStr2 = now.toString(template: "HH:mm:ss") // 17:47:09

귀하의 요구에 맞게 템플릿을 사용하십시오. 예와 문서 여기 필요한 템플릿을 구축하는 데 도움이됩니다.

메모

캐시를 원할 수도 있습니다 DateFormatter 당신이 그것을 사용할 계획이라면 TableView 예를 들어. 아이디어를 주려면 1000 개가 넘는 날짜를 반복했습니다. 0.5 초 위를 사용합니다 toString(template: String) 기능과 비교합니다 0.05 초 사용 myFormatter.string(from: Date).

NSDate *date         = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"]
NSString *dateString  = [df stringFromDate:date];
[df setDateFormat:@"hh:mm:ss"];
NSString *hoursString = [df stringFromDate:date];

그게 바로, 당신은 당신이 원하는 모든 것을 얻었습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top