2

I'm using SwiftDate framework (see link below) https://github.com/malcommac/SwiftDate

I'd like to print the date and time in current region/local/timeZone. I can achieve this by using the below code without using SwiftDate:

DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .short)

Since, SwiftDate provides a shared date formatter. I'd like to know if the same is possible with SwiftDate.

Felix Marianayagam
  • 2,634
  • 2
  • 9
  • 29
  • You don't need a framework for this https://stackoverflow.com/questions/28332946/how-do-i-get-the-current-date-in-short-format-in-swift/28347285?r=SearchResults&s=1|21.0072#28347285 – Leo Dabus Mar 08 '20 at 17:26
  • I know, and I have already given the code that does it without using a library. I've been reading about SwiftDate and I like it. Therefore, I wanted to know if SwiftDate library had a similar option. I didn't want to rewrite any code if it already exists. – Felix Marianayagam Mar 08 '20 at 17:47

2 Answers2

1

I use an extension to get localized date:

import SwiftDate

extension Date {

//you can specify region/timezone/locale to what you want
    var localizedDate: DateInRegion {
        return self.in(region: Region(calendar: Calendar.current, zone: Zones.current, locale: Locale.current))
    }
}

Then you use it like this (if you need system localized date, just ommit extension part):

let dateString = Date().localizedDate.toString(.custom("dd.MM.yyyy."))
Predrag Samardzic
  • 2,661
  • 15
  • 18
  • The outputs of DateFormatter.localizedString and your localizedDate do not match. I removed the .custom("dd.MM.yyyy.") format you had specified. Users of different regions use different date separators like ".", "/" etc. Therefore, I'd like the date format to be picked up from the device. – Felix Marianayagam Mar 08 '20 at 18:10
  • Oh sorry, I misunderstood. If you want the same output, you can use: Date().localizedDate.toString(.dateTime(.short)) – Predrag Samardzic Mar 08 '20 at 21:26
0

I tried predrag-samardzic code and it worked. Now that there are two options (to do the same job), one with NSDateFormatter and the other with SwiftDate, I thought of profiling them to see which one is better. Here's the code that I used to profile them:

func testNSDateFormatter() {
    for _ in 1...10000 {
        print("\(Date().toLocalizedString())")
    }
}

func testSwiftDate() {
    for _ in 1...10000 {
        print("\(Date().localizedDate.toString(.dateTime(.short)))")
    }
}

extension Date {
    func toLocalizedString(dateStyle: DateFormatter.Style = .short, timeStyle: DateFormatter.Style = .short) -> String {
        return DateFormatter.localizedString(from: self, dateStyle: dateStyle, timeStyle: timeStyle)
    }

    var localizedDate: DateInRegion {
         return self.in(region: Region(calendar: Calendar.current, zone: Zones.current, locale: Locale.current))
    }
}

Here's the screenshot from the profiler.

NSDateFormatter Vs SwiftDate - Time Profile

The result of profiling:

NSDateFormatter - 773x SwiftDate - 2674x

NSDateFormatter is approximately 3.45 times faster than SwiftDate. Based on the above, I'd recommend using NSDateFormatter over SwiftDate.

Felix Marianayagam
  • 2,634
  • 2
  • 9
  • 29