问题描述
我想以日期时间格式转换格式为"19-07-2018 08:10:24"的字符串.如果日期是今天,那么应该是"02:12 am".如果日期是昨天,则应为昨天".否则,它应为"DD/MM/yy"格式.目前,我的时间和日期包含在字符串formate中.
I want to Convert string of format "19-07-2018 08:10:24" in date time format. If the date is today then it should be "02:12 am". If the date is of yesterday then it should be like "Yesterday". Otherwise it should be in "DD/MM/yy" format. Currently I am having the time and date in string formate.
我想要字符串类型的最终答案.如果答案是"DD/MM/YY",则它应该为字符串格式.
I want the final answer in string type. If the answer is "DD/MM/YY", then it should be in string format.
推荐答案
使用日历日期功能:
func format(date: Date) -> String {
let calendar = Calendar.current
if calendar.isDateInToday(date) {
// process your "Today" format
return ...
}
if calendar.isDateInYesterday(date) {
// process your "Yesterday" format
return ...
}
if calendar.isDateInTomorrow(date) {
// process your "Tomorrow" format
return ...
}
// process your "full date/time format"
return ...
}
您可以在此处阅读有关日历功能的更多信息: https://developer.apple.com/documentation/foundation/calendar
You can read more about the Calendar functions here: https://developer.apple.com/documentation/foundation/calendar
这篇关于转换格式为"19-07-2018 08:10:24"的字符串在日期和时间上都不同的字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!