How do I format the current date for the user's locale?

后端 未结 4 738
难免孤独
难免孤独 2021-02-05 09:00

I have this code where I\'m trying to get the current date and format it in the current locale.

NSDate *now = [NSDate date];  //  gets current date
NSString *sNo         


        
4条回答
  •  情深已故
    2021-02-05 09:39

    Here's a Swift 5 version of @JiaYou's answer:

    func format(date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
        return formatter.string(from: date)
    }
    

    Here I need date only, so I indicate the timeStyle as none.

提交回复
热议问题