Develop
2018.10.18 06:41
[ios] Swift 4 String, Date, DateFormatter 예제
Views 1521 Comment 0
Swift 4, Swift 3 에서 문자열을 Date() 읽고.
Date()를 DateFormatter로 문자열 출력하는 간단한 예제
1. Extensions 필요함
import Foundation
extension DateFormatter {
convenience init (format: String) {
self.init()
dateFormat = format
locale = Locale.current
}
}
extension String {
func toDate (format: String) -> Date? {
return DateFormatter(format: format).date(from: self)
}
func toDateString (inputFormat: String, outputFormat:String) -> String? {
if let date = toDate(format: inputFormat) {
return DateFormatter(format: outputFormat).string(from: date)
}
return nil
}
}
extension Date {
func toString (format:String) -> String? {
return DateFormatter(format: format).string(from: self)
}
}
2. 실행 예
var dateString = "14.01.2017T14:54:00"
let format = "dd.MM.yyyy'T'HH:mm:ss"
let date = Date()
print("original String with date: \(dateString)")
print("date String() to Date(): \(dateString.toDate(format: format)!)")
print("date String() to formated date String(): \(dateString.toDateString(inputFormat: format, outputFormat: "dd MMMM")!)")
print("format Date(): \(date.toString(format: "dd MMM HH:mm")!)")
[출처] https://stackoverflow.com/questions/24777496/how-can-i-convert-string-date-to-nsdate
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
57 | Develop | [ajax] 샘플 코드와 한글처리에 대한 간단한 설명 | hooni | 2013.04.23 | 6878 |
56 | Develop |
ZBar 라이브러리를 이용한 바코드 스캔 앱 개발하기
![]() |
hooni | 2015.01.01 | 1737 |
55 | Develop |
XML, JSON, BSON, MSGPACK 장,단점 비교
![]() |
hooni | 2017.01.11 | 2386 |
54 | Develop |
XE Core 1.8.18 본문 작성시 태그(html) 사라지는 버그
![]() |
hooni | 2016.04.21 | 1033 |
53 | Etc | WM미통기 - 10. 조건부확률 | hooni | 2015.04.20 | 815 |
52 | Develop | What is difference between Get, Post, Put and Delete? | hooni | 2018.02.28 | 1578 |
51 | Develop |
URI 인코딩, URL 인코딩
![]() |
hooni | 2013.04.23 | 18907 |
50 | System/OS |
SVN(Subversion) 설치와 설정 (sasl 인증 적용 포함)
![]() |
hooni | 2014.09.11 | 5855 |
49 | Develop | SVN 초간단 사용하기 | hooni | 2014.02.28 | 7684 |
48 | Develop | SVN 명령어 (SVN command) | hooni | 2014.02.28 | 12232 |
47 | System/OS |
SSH Passwordless Login Using SSH Keygen in 5 Easy Steps
![]() |
hooni | 2019.11.22 | 1550 |
46 | Database |
SQL JOIN 정리 (Inner Join & Outer Join)
![]() |
hooni | 2019.11.22 | 1960 |