Photo by Caspar Camille Rubin / Unsplash

Date Formatter: iOS 15 ile gelen yeni zaman formatları

Swift Apr 9, 2022

iOS 15 ile farklı şekillerde zaman formatları oluşturulabilir. Aşağıda da yeni kullanım şekilleri ile ilgili örnekler gösterilmiştir.

let now = Date()
print(now.formatted())
// 4/5/2022, 8:00 PM

print(now.formatted(date: .complete, time: .omitted))
// Tuesday, April 5, 2022

print(now.formatted(date: .abbreviated, time: .standard))
// Apr 5, 2022, 8:00:00 PM
Genel yapıc

let now = Date()
print(now.formatted(date: .abbreviated, time: .omitted))
// Apr 5, 2022

print(now.formatted(date: .complete, time: .omitted))
// Tuesday, April 5, 2022

print(now.formatted(date: .long, time: .omitted))
// April 5, 2022

print(now.formatted(date: .numeric, time: .omitted))
// 4/5/2022
Farklı Date tiplerinde örnekler

let now = Date()
print(now.formatted(date: .omitted, time: .complete))    
// 2:15:36 PM GMT

print(now.formatted(date: .omitted, time: .shortened))
// 2:15 PM

print(now.formatted(date: .omitted, time: .standard))
// 2:15:36 PM
Farklı Time tipinde örnekler

let now = Date()
print(now.formatted(.iso8601))
// 2022-04-05T14:15:36Z

print(now.formatted(.iso8601.time(includingFractionalSeconds: true)))
// 14:15:36.000

print(now.formatted(.iso8601.dateTimeSeparator(.space)))
// 2022-04-05 14:15:36Z
iso 8601 formatı

let now = Date()
print(now.formatted(.dateTime.year().day().month()))
// Apr 05, 2022

print(now.formatted(.dateTime.hour().minute().month().day()))
// Apr 05, 2:15 PM

let tr_TR = Locale(identifier: "tr_TR")
print(now.formatted(.dateTime.year().day().month().locale(tr_TR))) 
// 05 Nis 2022

print(now.formatted(.dateTime.year().month()))
// Apr 2022

print(now.formatted(.dateTime.year(.twoDigits).month(.wide)))
// April 22
Seçtiğin alanlara göre Date format oluşturma

let now = Date()
now.formatted(.dateTime.day())                     
// 23

now.formatted(.dateTime.day(.ordinalOfDayInMonth)) 
// 4 (4th week)

now.formatted(.dateTime.dayOfYear(.threeDigits))  
// 023

now.formatted(.dateTime.era(.wide))              
// Anno Domini

now.formatted(.dateTime.hour(.twoDigits(amPM: .narrow)))
// 02 p 

now.formatted(.dateTime.quarter())               
// Q1

now.formatted(.dateTime.timeZone(.exemplarLocation))
// London

now.formatted(.dateTime.week(.weekOfMonth))     
// 5

now.formatted(.dateTime.weekday(.short))      
// Su
Diğer kullanım örnekleri

Tags

Furkan Ozoglu

iOS Developer & Geomatics Engineer