格式化一天中的时间 Swift Morning/Afternoon/Evening/Any 时间
Formatting time of the day Swift Morning/Afternoon/Evening/Any time
我正在尝试找到一种方法来获取一天中的时间。显然有一个简单的方法可以做到 Application to Display Morning,Evening
如果您对一种语言的静态词没问题。有没有办法根据语言环境来制作它? NSDateComponentsFormatter 似乎无法解决问题。
实际上,您可以像这样在 NSDateFormatter
中设置语言环境:
let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
此日期格式化程序将帮助您在 currentLocale
中打印日期。
但对于您所期望的,您必须实施本地化才能获得 "Morning"、"Afternoon"、"Night" 字符串的本地化字符串。
充其量,您可以对您的约会对象执行此操作:
NSDateFormatter
-doesRelativeDateFormatting
中有一个属性。它会将日期格式化为正确语言环境中的相对日期。
If a date formatter uses relative date formatting, where possible it
replaces the date component of its output with a phrase—such as
“today” or “tomorrow”—that indicates a relative date. The available
phrases depend on the locale for the date formatter; whereas, for
dates in the future, English may only allow “tomorrow,” French may
allow “the day after the day after tomorrow,” as illustrated in the
following example.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
dateFormatter.locale = frLocale;
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString: %@", dateString);
// Output
// dateString: après-après-demain
以下是我使用 Swift 解决问题的方法 2. 首先,我使用 this article 来识别一天中的不同时段。从那里开始,我使用了一系列 if/else if 语句。 我很好奇其他人是否可以使用范围来做到这一点。
//BIG PICTURE SOLUTION
//Step 1: Build a .plist or REST API service or whatever made up of different ways to describe "parts of the day" in different languages.
//
//List of Language Codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
//Step 2: Get the user's local time zone
//Step 3: Calculate whether the user's local time fits within these buckets of time
import Foundation
class DayParts{
var currentHour:Int
var localLang:String?
// IDEA: Build a .plist or a REST API service or whatever that simply returns a dictiontary
let letterCodes:[String:Array<String>] = [
"en": ["Early Morning", "Late Morning", "Early Afternoon", "Late Afternoon", "Evening", "Night"],
"fr": ["Tôt le matin", "Tard dans la matinée", "Début d'après-midi", "Tard dans l'après-midi", "Soir", "Nuit"],
"es": ["Mañana Temprano", "Mañana tarde", "Temprano en la tarde", "Fin de la tarde", "Anochecer", "Noche"]
]
init(){
//A. Get the current time
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH"
//B. Get the current hour
currentHour = Int(dateFormatter.stringFromDate(date))!
//C. Get the current phone language
localLang = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as? String
}
func now() -> String {
if(currentHour < 08){
return letterCodes[localLang!]![0]
}
else if(currentHour < 11){
return letterCodes[localLang!]![1]
}
else if( currentHour < 15){
return letterCodes[localLang!]![2]
}
else if( currentHour < 17){
return letterCodes[localLang!]![3]
}
else if(currentHour < 21){
return letterCodes[localLang!]![4]
}
else{
return "Night"
}
}
}
let dayParts = DayParts().now()
不幸的是,没有内置的解决方案——NSDateFormatter 的相对格式仅适用于每天。
使用 Calendar.current.component(.hour, from: Date())
获取小时并使用范围开关和 NSLocalizedString()
本地化字符串。
例如:
// let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate()) Swift 2 legacy
let hour = Calendar.current.component(.hour, from: Date())
switch hour {
case 6..<12 : print(NSLocalizedString("Morning", comment: "Morning"))
case 12 : print(NSLocalizedString("Noon", comment: "Noon"))
case 13..<17 : print(NSLocalizedString("Afternoon", comment: "Afternoon"))
case 17..<22 : print(NSLocalizedString("Evening", comment: "Evening"))
default: print(NSLocalizedString("Night", comment: "Night"))
}
创建一个文件localizable.strings
并添加您需要的本地化。
我正在尝试找到一种方法来获取一天中的时间。显然有一个简单的方法可以做到 Application to Display Morning,Evening 如果您对一种语言的静态词没问题。有没有办法根据语言环境来制作它? NSDateComponentsFormatter 似乎无法解决问题。
实际上,您可以像这样在 NSDateFormatter
中设置语言环境:
let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
此日期格式化程序将帮助您在 currentLocale
中打印日期。
但对于您所期望的,您必须实施本地化才能获得 "Morning"、"Afternoon"、"Night" 字符串的本地化字符串。
充其量,您可以对您的约会对象执行此操作:
NSDateFormatter
-doesRelativeDateFormatting
中有一个属性。它会将日期格式化为正确语言环境中的相对日期。
If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas, for dates in the future, English may only allow “tomorrow,” French may allow “the day after the day after tomorrow,” as illustrated in the following example.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
dateFormatter.locale = frLocale;
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString: %@", dateString);
// Output
// dateString: après-après-demain
以下是我使用 Swift 解决问题的方法 2. 首先,我使用 this article 来识别一天中的不同时段。从那里开始,我使用了一系列 if/else if 语句。 我很好奇其他人是否可以使用范围来做到这一点。
//BIG PICTURE SOLUTION
//Step 1: Build a .plist or REST API service or whatever made up of different ways to describe "parts of the day" in different languages.
//
//List of Language Codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
//Step 2: Get the user's local time zone
//Step 3: Calculate whether the user's local time fits within these buckets of time
import Foundation
class DayParts{
var currentHour:Int
var localLang:String?
// IDEA: Build a .plist or a REST API service or whatever that simply returns a dictiontary
let letterCodes:[String:Array<String>] = [
"en": ["Early Morning", "Late Morning", "Early Afternoon", "Late Afternoon", "Evening", "Night"],
"fr": ["Tôt le matin", "Tard dans la matinée", "Début d'après-midi", "Tard dans l'après-midi", "Soir", "Nuit"],
"es": ["Mañana Temprano", "Mañana tarde", "Temprano en la tarde", "Fin de la tarde", "Anochecer", "Noche"]
]
init(){
//A. Get the current time
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH"
//B. Get the current hour
currentHour = Int(dateFormatter.stringFromDate(date))!
//C. Get the current phone language
localLang = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as? String
}
func now() -> String {
if(currentHour < 08){
return letterCodes[localLang!]![0]
}
else if(currentHour < 11){
return letterCodes[localLang!]![1]
}
else if( currentHour < 15){
return letterCodes[localLang!]![2]
}
else if( currentHour < 17){
return letterCodes[localLang!]![3]
}
else if(currentHour < 21){
return letterCodes[localLang!]![4]
}
else{
return "Night"
}
}
}
let dayParts = DayParts().now()
不幸的是,没有内置的解决方案——NSDateFormatter 的相对格式仅适用于每天。
使用 Calendar.current.component(.hour, from: Date())
获取小时并使用范围开关和 NSLocalizedString()
本地化字符串。
例如:
// let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate()) Swift 2 legacy
let hour = Calendar.current.component(.hour, from: Date())
switch hour {
case 6..<12 : print(NSLocalizedString("Morning", comment: "Morning"))
case 12 : print(NSLocalizedString("Noon", comment: "Noon"))
case 13..<17 : print(NSLocalizedString("Afternoon", comment: "Afternoon"))
case 17..<22 : print(NSLocalizedString("Evening", comment: "Evening"))
default: print(NSLocalizedString("Night", comment: "Night"))
}
创建一个文件localizable.strings
并添加您需要的本地化。