Created
January 11, 2021 23:33
-
-
Save eduzera/1c2f5ce4c8698825b8e2c6369a680587 to your computer and use it in GitHub Desktop.
Swift Date and Currency Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// 1. Current Date | |
let date = Date() | |
// 2. Create Formatter | |
let dateFormatter = DateFormatter() | |
// 3. Display Date short format | |
print("---") | |
// 3.1 Portuguese Brazil Locale (pt_BR) | |
dateFormatter.locale = Locale(identifier: "pt_BR") | |
dateFormatter.setLocalizedDateFormatFromTemplate("dd/MM/yyyy HH:mm") // // set template after setting locale | |
print("[BR] \(dateFormatter.string(from: date))") | |
// 3.2 US English Locale (en_US) | |
dateFormatter.locale = Locale(identifier: "en_US") | |
dateFormatter.setLocalizedDateFormatFromTemplate("MM/dd/yyyy hh:mm") // set template after setting locale | |
print("[US] \(dateFormatter.string(from: date))") | |
// 4 Display Date medium format | |
print("---") | |
// 4.1 Portuguese Brazil Locale (pt_BR) | |
dateFormatter.locale = Locale(identifier: "pt_BR") | |
dateFormatter.setLocalizedDateFormatFromTemplate("dd MMM - HH:mm") // // set template after setting locale | |
print("[BR] \(dateFormatter.string(from: date))") | |
// 4.2 US English Locale (en_US) | |
dateFormatter.locale = Locale(identifier: "en_US") | |
dateFormatter.setLocalizedDateFormatFromTemplate("dd MMM - hh:mm") // // set template after setting locale | |
print("[US] \(dateFormatter.string(from: date))") | |
// 5 Display Date large format | |
print("---") | |
// 5.1 Portuguese Brazil Locale (pt_BR) | |
dateFormatter.locale = Locale(identifier: "pt_BR") | |
dateFormatter.dateFormat = "EEEE dd MMM 'às' HH:mm" // // set template after setting locale | |
print("[BR] \(dateFormatter.string(from: date))") | |
// 5.2 US English Locale (en_US) | |
dateFormatter.locale = Locale(identifier: "en_US") | |
dateFormatter.dateFormat = "EEEE dd MMM 'at' hh:mm" // // set template after setting locale | |
print("[US] \(dateFormatter.string(from: date))") | |
// 6 Display Currency Format | |
print("---") | |
let numbers = [1.0, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0] | |
for number in numbers { | |
print("---") | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .currency | |
// 6.1 pt-BR | |
formatter.locale = Locale(identifier: "pt_BR") | |
print("[BR] \(formatter.string(from: number as NSNumber)!)") | |
// 6.2 US | |
formatter.locale = Locale(identifier: "en_US") | |
print("[US] \(formatter.string(from: number as NSNumber)!)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
[BR] 11/01/2021 20:32 | |
[US] 01/11/2021, 8:32 PM | |
--- | |
[BR] 11 de jan 20:32 | |
[US] Jan 11, 8:32 PM | |
--- | |
[BR] segunda-feira 11 jan às 20:32 | |
[US] Monday 11 Jan at 08:32 | |
--- | |
[BR] R$ 1,00 | |
[US] $1.00 | |
--- | |
[BR] R$ 10,00 | |
[US] $10.00 | |
--- | |
[BR] R$ 100,00 | |
[US] $100.00 | |
--- | |
[BR] R$ 1.000,00 | |
[US] $1,000.00 | |
--- | |
[BR] R$ 10.000,00 | |
[US] $10,000.00 | |
--- | |
[BR] R$ 100.000,00 | |
[US] $100,000.00 | |
--- | |
[BR] R$ 1.000.000,00 | |
[US] $1,000,000.00 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment