'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

2018-12-24 18:00:00 UTC

How can I get from this day or month?



Solution 1:[1]

Details

  • Xcode Version 11.0 (11A420a), Swift 5

Links

How to convert string to Date

Solution

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

Usage

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

Solution 2:[2]

Is that String or Date type?

if it is date you can do so:

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

that will give you the day, year and month

Solution 3:[3]

If you saved "2018-12-24 18:00:00 UTC" as a String, you can try this:

    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!

Solution 4:[4]

The date string is not ISO8601 compliant, nevertheless you can use ISO8601DateFormatter if you replace <space> + UTC with Z and use custom format options.

Create a Date from the string and get the DateComponents for day and month

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

Solution 5:[5]

This is how I did in swift 4. I have a Billing class with "billDate" a field that contains the date.

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

Solution 6:[6]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jmartinalonso
Solution 3 GorCat
Solution 4
Solution 5 Amit Baderia
Solution 6 ZygD