如何从日期类型获取日和月- swift 4

uujelgoq  于 2023-01-12  发布在  Swift
关注(0)|答案(8)|浏览(170)

我有一个数据类型为Date的变量,其中我以以下格式存储日期
世界协调时2018年12月24日18:00:00
从这一天或这一个月我怎么能得到?

sqougxex

sqougxex1#

详情

  • Xcode版本11.0(11A420a),Swift 5

链接

如何将字符串转换为日期
溶液

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))")
1rhkuytd

1rhkuytd2#

是字符串类型还是日期类型?
如果是日期,则可以执行以下操作:

let calendarDate = Calendar.current.dateComponents([.day, .year, .month], from: date)

它会给予你日期,年份和月份

gjmwrych

gjmwrych3#

如果您将“2018-12-24 18:00:00 UTC”另存为String,您可以尝试以下操作:

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

最美好的祝愿!

vawmfj5a

vawmfj5a4#

日期字符串不符合ISO8601标准,但如果将<space> + UTC替换为Z并使用自定义格式选项,则可以使用ISO8601DateFormatter
从字符串创建一个Date,并获取daymonthDateComponents

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)
}
h22fl7wq

h22fl7wq5#

这是我在swift 4中所做的。我有一个Billing类,其中“billDate”是一个包含日期的字段。

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 ""
}
voj3qocg

voj3qocg6#

扩展日期所有时间返回字符串:

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
}
}
llmtgqce

llmtgqce7#

extension Date {
   func getRemainingDayMonthYear (_ startDate: Date = Date()) -> String 
    {
        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
    }
}
xggvc2p6

xggvc2p68#

如果您有一个日期变量,您也可以将此解决方案用于SwiftUi:

let date = Date()

Text(date, style: .time) // January 8, 2023
Text(date, style: .date) // 12:08 PM

相关问题