본문 바로가기
프로그래밍/iOS-Swift

[SWIFT] 오늘 날짜 & 시간, swift string to date, swift date to string

by 채연2 2021. 7. 12.

 

 

 

이번 포스팅에서는 date 관련해서 다뤄보려고 한다.

 

 

현재 날짜 & 시간 구하기

func getNowTime() -> String {
	let now = Date()
	let dateFormatter = DateFormatter()
	dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
	dateFormatter.timeZone = NSTimeZone(name: "ko_KR") as TimeZone?

	return dateFormatter.string(from: now)
}

 

 

String to Date

func getStringToDate(strDate:String, format:String) -> Date {
	let dateFormatter = DateFormatter()
	dateFormatter.dateFormat = format
	dateFormatter.timeZone = NSTimeZone(name: "ko_KR") as TimeZone?

	return dateFormatter.date(from: strDate)!
}

 

 

 

320x100

 

 

Date to String

func getDateToString(date:Date, format:String) -> String{
	let dateFormatter = DateFormatter()
	dateFormatter.dateFormat = format
	dateFormatter.timeZone = NSTimeZone(name: "ko_KR") as TimeZone?

	return dateFormatter.string(from: date)
}

 

 

 

 

320x100

댓글