SwiftDate icon indicating copy to clipboard operation
SwiftDate copied to clipboard

Help needed for Time Comparison

Open HermanKayy opened this issue 5 years ago • 2 comments

I have a date I want to compare to with the current date. If it's a year I want it to say "1 year ago", if it's month then I want i to say "1 month ago", "week ago, min ago, hour ago, etc" How can I achieve this in SwiftDate? I looked at the comparison docs and couldn't find anything. Can anyone help and provide an example?

HermanKayy avatar Dec 27 '20 07:12 HermanKayy

You don't need SwiftDate for this functionality. You can use RelativeDateTimeFormatter since iOS 13.

Example:

let now = Date()
let dateFormatter = RelativeDateTimeFormatter()

let oneMonthAgo = Calendar.current.date(byAdding: .month, value: -1, to: now) ?? Date()
let result = dateFormatter.localizedString(for: oneMonthAgo, relativeTo: now)

Results:
// en_US: "4 weeks ago"
// es_ES: "hace 4 semanas"
// zh_TW: "4 週前"

ghost avatar Dec 28 '20 12:12 ghost

@HermanKayy Please use this to have some idea:

// This function group all invoices by their relative string
    func groupInvoicesByDate(Invoices invoices: [Invoice]) -> [InvoiceGroup] {
        
        var groups = [InvoiceGroup]()
        var invoicesCopy = invoices
        
        var today = InvoiceGroup(header: "Hoy", invoices: [])
        var yesterday = InvoiceGroup(header: "Ayer", invoices: [])
        var thisWeek = InvoiceGroup(header: "Hace una semana", invoices: [])
        var fifteenDays = InvoiceGroup(header: "Hace quince días", invoices: [])
        var thisMonth = InvoiceGroup(header: "Hace un mes", invoices: [])
        var threeMonths = InvoiceGroup(header: "Hace tres meses", invoices: [])
        var sixMonths = InvoiceGroup(header: "Hace seis meses", invoices: [])
        var thisYear = InvoiceGroup(header: "Hace un año", invoices: [])
        var lastYear = InvoiceGroup(header: "El año pasado", invoices: [])
        var olds = InvoiceGroup(header: "Antiguas", invoices: [])
        
        while invoicesCopy.count > 0 {
            
            let currentInvoice = invoicesCopy.first!
            
            // Today
            if currentInvoice.createdAt!.compare(.isToday) {
                today.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // Yesterday
            if currentInvoice.createdAt!.compare(.isYesterday) {
                yesterday.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // This week
            if currentInvoice.createdAt!.isAfterDate(Date() - 7.days, orEqual: true, granularity: .second) {
                thisWeek.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // 15 days
            if currentInvoice.createdAt!.isAfterDate(Date() - 15.days, orEqual: true, granularity: .second) {
                fifteenDays.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // 1 month
            if currentInvoice.createdAt!.isAfterDate(Date() - 1.months, orEqual: true, granularity: .second) {
                thisMonth.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // 3 months
            if currentInvoice.createdAt!.isAfterDate(Date() - 3.months, orEqual: true, granularity: .second) {
                threeMonths.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // 6 months
            if currentInvoice.createdAt!.isAfterDate(Date() - 6.months, orEqual: true, granularity: .second) {
                sixMonths.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // 1 year
            if currentInvoice.createdAt!.isAfterDate(Date() - 1.years, orEqual: true, granularity: .second) {
                thisYear.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // Last year
            if currentInvoice.createdAt!.compare(.isLastYear) {
                lastYear.invoices.append(currentInvoice)
                invoicesCopy.removeFirst()
                continue
            }
            
            // Is too old
            olds.invoices.append(currentInvoice)
            invoicesCopy.removeFirst()
        }
        
        today.header = "\(today.header) (\(today.invoices.count))"
        yesterday.header = "\(yesterday.header) (\(yesterday.invoices.count))"
        thisWeek.header = "\(thisWeek.header) (\(thisWeek.invoices.count))"
        fifteenDays.header = "\(fifteenDays.header) (\(fifteenDays.invoices.count))"
        thisMonth.header = "\(thisMonth.header) (\(thisMonth.invoices.count))"
        threeMonths.header = "\(threeMonths.header) (\(threeMonths.invoices.count))"
        sixMonths.header = "\(sixMonths.header) (\(sixMonths.invoices.count))"
        thisYear.header = "\(thisYear.header) (\(thisYear.invoices.count))"
        lastYear.header = "\(lastYear.header) (\(lastYear.invoices.count))"
        olds.header = "\(olds.header) (\(olds.invoices.count))"
        
        if today.invoices.count > 0 {
            groups.append(today)
        }
        
        if yesterday.invoices.count > 0 {
            groups.append(yesterday)
        }
        
        if thisWeek.invoices.count > 0 {
            groups.append(thisWeek)
        }
        
        if fifteenDays.invoices.count > 0 {
            groups.append(fifteenDays)
        }
        
        if thisMonth.invoices.count > 0 {
            groups.append(thisMonth)
        }
        
        if threeMonths.invoices.count > 0 {
            groups.append(threeMonths)
        }
        
        if sixMonths.invoices.count > 0 {
            groups.append(sixMonths)
        }
        
        if thisYear.invoices.count > 0 {
            groups.append(thisYear)
        }
        
        if lastYear.invoices.count > 0 {
            groups.append(lastYear)
        }
        
        if olds.invoices.count > 0 {
            groups.append(olds)
        }
        
        return groups
    }

wonder2011 avatar Jun 29 '22 21:06 wonder2011