NSDate를 연도, 월, 일 및시, 분, 초 모두에 대해 특정 스타일로 포맷
기본적으로 현재 날짜와 시간을 별도로 형식화해야합니다.
2009-04-26 11:06:54
동일한 주제에 대한 다른 질문에서 아래 코드는 다음을 생성합니다.
지금 : | 2009-06-01 23:18:23 +0100 | dateString : | 2009 년 6 월 1 일 23 : 18 | 구문 분석 : | 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"];
set the format to return is....
yyyy-MM-dd
return 2015-12-17
date
yyyy-MMM-dd
return 2015-Dec-17
date
yy-MM-dd
return 15-12-17
date
dd-MM-yy
return 17-12-15
date
dd-MM-yyyy
return 17-12-2015
date
yyyy-MMM-dd HH:mm:ss
return 2015-Dec-17 08:07:13
date and time
yyyy-MMM-dd HH:mm
return 2015-Dec-17 08:07
date and time
For more Details Data and Time Format for Click Now.
Thank you.....
nothing new but still want to share my method:
+(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;
}
For 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)!;
Swift 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)
}
}
Usage
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
Play with template to match your needs. Examples and doc here to help you build the template you need.
Note
You may want to cache your DateFormatter
if you plan to use it in TableView
for instance. To give an idea, looping over 1000 dates took me 0.5 sec using the above toString(template: String)
function, compared to 0.05 sec using 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];
Thats it, you got it all you want.
'Programming' 카테고리의 다른 글
Python 프로그램을 C / C ++ 코드로 변환 하시겠습니까? (0) | 2020.06.28 |
---|---|
Hamcrest에서 무언가가 null인지 어떻게 주장합니까? (0) | 2020.06.28 |
한 명의 사용자가 수정 한 모든 파일을 git에게 알려줄 수 있습니까? (0) | 2020.06.28 |
VARCHAR (255)이 너무 자주 사용되는 다른 이유가 있습니까? (0) | 2020.06.28 |
postgres 데이터베이스에서 단일 테이블의 백업을 만드는 방법은 무엇입니까? (0) | 2020.06.28 |