[英]How to get day and month from Date type - swift 4
I have variable with data type Date, where I have stored date in this format我有数据类型为 Date 的变量,我在其中存储了这种格式的日期
2018-12-24 18:00:00 UTC
2018-12-24 18:00:00 UTC
How can I get from this day or month?如何从这一天或这一月得到?
How to convert string to Date 如何将字符串转换为日期
extension Date {
func get(_ components: Calendar.Component..., calendar: Calendar = Calendar.current) -> DateComponents {
return calendar.dateComponents(Set(components), from: self)
}
func get(_ component: Calendar.Component, calendar: Calendar = Calendar.current) -> Int {
return calendar.component(component, from: self)
}
}
let date = Date()
// MARK: Way 1
let components = date.get(.day, .month, .year)
if let day = components.day, let month = components.month, let year = components.year {
print("day: \(day), month: \(month), year: \(year)")
}
// MARK: Way 2
print("day: \(date.get(.day)), month: \(date.get(.month)), year: \(date.get(.year))")
Is that String or Date type?那是 String 还是 Date 类型?
if it is date you can do so:如果是日期,您可以这样做:
let calanderDate = Calendar.current.dateComponents([.day, .year, .month], from: date)
that will give you the day, year and month这会给你日、年和月
If you saved "2018-12-24 18:00:00 UTC" as a String, you can try this:如果您将“2018-12-24 18:00:00 UTC”保存为字符串,则可以尝试以下操作:
let dateString = "2018-12-24 18:00:00 UTC"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss 'UTC'"
guard let date = formatter.date(from: dateString) else {
return
}
formatter.dateFormat = "yyyy"
let year = formatter.string(from: date)
formatter.dateFormat = "MM"
let month = formatter.string(from: date)
formatter.dateFormat = "dd"
let day = formatter.string(from: date)
print(year, month, day) // 2018 12 24
Best Wishes!最好的祝愿!
The date string is not ISO8601 compliant, nevertheless you can use ISO8601DateFormatter
if you replace <space> + UTC
with Z
and use custom format options.日期字符串不符合 ISO8601,但如果您将
<space> + UTC
替换为Z
并使用自定义格式选项,则可以使用ISO8601DateFormatter
。
Create a Date
from the string and get the DateComponents
for day
and month
从字符串创建一个
Date
并获取day
和month
的DateComponents
let string = "2018-12-24 18:00:00 UTC"
let formatter = ISO8601DateFormatter()
let trimmedString = string.replacingOccurrences(of: "\\s?UTC", with: "Z", options: .regularExpression)
formatter.formatOptions = [.withFullDate, .withFullTime, .withSpaceBetweenDateAndTime]
if let date = formatter.date(from: trimmedString) {
let components = Calendar.current.dateComponents([.day, .month], from: date)
let day = components.day!
let month = components.month!
print(day, month)
}
This is how I did in swift 4. I have a Billing class with "billDate" a field that contains the date.这就是我在 swift 4 中所做的。我有一个带有“billDate”的 Billing 类,一个包含日期的字段。
extension Billing:Identifiable{
func month() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM"
if let inputDate = billDate {
let month = dateFormatter.string(from: inputDate)
return month.uppercased()
}
return ""
}
Extention Date All Time RETURN String:延期日期所有时间返回字符串:
extension Date {
func dateDistance(_ startDate: Date = Date()) -> String {
var remainingTime = ""
let dateValue = Calendar.current.dateComponents([.year,.month,.weekOfMonth,.day,.hour,.minute,.second], from: startDate, to: self)
remainingTime = String(format: "%2d Y - %2d M - %2d W - %2d D\n%02d:%02d:%02d",
dateValue.year ?? 0,
dateValue.month ?? 0,
dateValue.weekOfMonth ?? 0,
dateValue.day ?? 0,
dateValue.hour ?? 0,
dateValue.minute ?? 0,
dateValue.second ?? 0
)
return remainingTime
}
}
extension Date {延期日期{
func getRemainingDayMonthYear (_ startDate: Date = Date()) -> String func getRemainingDayMonthYear (_ startDate: Date = Date()) -> 字符串
{ var remainingTime = "" let dateValue = Calendar.current.dateComponents([.year,.month,.weekOfMonth,.day,.hour,.minute,.second], from: startDate, to: self) { var remainingTime = "" let dateValue = Calendar.current.dateComponents([.year,.month,.weekOfMonth,.day,.hour,.minute,.second], from: startDate, to: self)
if dateValue.year ?? 0 > 0 {
remainingTime = dateValue.year ?? 0 > 1 ? String(format: "%2d Years",dateValue.year ?? 0) : String(format: "%2d Year",dateValue.year ?? 0)
} else if dateValue.month ?? 0 > 0 {
remainingTime = dateValue.month ?? 0 > 1 ? String(format: "%2d Months",dateValue.month ?? 0) : String(format: "%2d Month",dateValue.month ?? 0)
} else {enter code here
remainingTime = dateValue.day ?? 0 > 1 ? String(format: "%2d Days",dateValue.day ?? 0) : String(format: "%2d Day",dateValue.day ?? 0)
}
return remainingTime
}
} }
If you have a date variable you can also use this solution for SwiftUi:如果您有日期变量,您也可以将此解决方案用于 SwiftUi:
let date = Date()
Text(date, style: .time) // January 8, 2023
Text(date, style: .date) // 12:08 PM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.