40

在下图中,您可以看到我编写的代码和所有变量的值:

class fun getCurrentShortDate() -> String {
    var todaysDate = NSDate()
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    var DateInFormat = dateFormatter.stringFromDate(todaysDate)

    return DateInFormat
}

变量值

如您所见,当前日期没有问题,但是当我尝试将 NSDate 更改为字符串时,它就不会这样做。

4

8 回答 8

58

Xcode 11 或更高版本 • Swift 5.1 或更高版本


extension TimeZone {
    static let gmt = TimeZone(secondsFromGMT: 0)!
}

extension Locale {
    static let ptBR = Locale(identifier: "pt_BR")
}

extension Formatter {
    static let date = DateFormatter()
}

extension Date {
    func localizedDescription(date dateStyle: DateFormatter.Style = .medium,
                              time timeStyle: DateFormatter.Style = .medium,
                              in timeZone: TimeZone = .current,
                              locale: Locale = .current,
                              using calendar: Calendar = .current) -> String {
        Formatter.date.calendar = calendar
        Formatter.date.locale = locale
        Formatter.date.timeZone = timeZone
        Formatter.date.dateStyle = dateStyle
        Formatter.date.timeStyle = timeStyle
        return Formatter.date.string(from: self)
    }
    var localizedDescription: String { localizedDescription() }
}

Date().localizedDescription  // "18 Dec 2021 06:43:50"
Date().localizedDescription(in: .gmt)  // "18 Dec 2021 06:43:50" UTC time
Date().localizedDescription(date: .short, time: .short)  // "18/12/21 03:43"
Date().localizedDescription(date: .full, time: .full)  // "Saturday, 18 December 2021 03:43:50 Brasilia Standard Time"
Date().localizedDescription(date: .full, time: .full, in: .gmt)  // "Saturday, 18 December 2021 06:43:50 Greenwich Mean Time"
Date().localizedDescription(date: .full, time: .full, locale: .ptBR)  // "sábado, 18 de dezembro de 2021 03:43:50 Horário Padrão de Brasília"

extension Date {

    var fullDate: String { localizedDescription(date: .full, time: .none) }
    var longDate: String { localizedDescription(date: .long, time: .none) }
    var mediumDate: String { localizedDescription(date: .medium, time: .none) }
    var shortDate: String { localizedDescription(date: .short, time: .none) }

    var fullTime: String { localizedDescription(date: .none, time: .full) }
    var longTime: String { localizedDescription(date: .none, time: .long) }
    var mediumTime: String { localizedDescription(date: .none, time: .medium) }
    var shortTime: String { localizedDescription(date: .none, time: .short) }

    var fullDateTime: String { localizedDescription(date: .full, time: .full) }
    var longDateTime: String { localizedDescription(date: .long, time: .long) }
    var mediumDateTime: String { localizedDescription(date: .medium, time: .medium) }
    var shortDateTime: String { localizedDescription(date: .short, time: .short) }
}

print(Date().fullDate)  // "Saturday, 18 December 2021\n"
print(Date().shortDate)  // "18/12/21\n"

print(Date().fullTime)  // "03:43:50 Brasilia Standard Time\n"
print(Date().shortTime)  // "03:43\n"

print(Date().fullDateTime)  // "Saturday, 18 December 2021 03:43:50 Brasilia Standard Time\n"
print(Date().shortDateTime)  // "18/12/21 03:43\n"
于 2015-02-05T15:03:40.817 回答
15

Swift 3.0 更新:Date

extension Date {
    func string(format: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

用法:

Date().string(format: "yyyy-MM-dd")

Swift 2.2:NSDate

extension NSDate {  
    func dateStringWithFormat(format: String) -> String {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.stringFromDate(self)
    }   
}

用法:

NSDate().dateStringWithFormat("yyyy-MM-dd")
于 2016-02-03T07:04:26.407 回答
15

斯威夫特 4 或 5

extension Date {

 static func getCurrentDate() -> String {

        let dateFormatter = DateFormatter()

        dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"

        return dateFormatter.string(from: Date())

    }
}

使用

print(Date.getCurrentDate())
于 2019-03-27T13:48:34.330 回答
10

您可以创建一个扩展以轻松将 aString转换为NSDate.

extension NSDate {
    func dateFromString(date: String, format: String) -> NSDate {
        let formatter = NSDateFormatter()
        let locale = NSLocale(localeIdentifier: "en_US_POSIX")

        formatter.locale = locale
        formatter.dateFormat = format

        return formatter.dateFromString(date)!
    }
}

比在你的函数中你可以NSDate().dateFromString("2015-02-04 23:29:28", format: "yyyy-MM-dd HH:mm:ss")而且这应该有效。输入日期不需要与输出日期的格式相同。

func getCurrentShortDate() -> String {
    var todaysDate = NSDate().dateFromString("2015-02-04 23:29:28", format:  "yyyy-MM-dd HH:mm:ss")

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    var DateInFormat = dateFormatter.stringFromDate(todaysDate)

    return DateInFormat
}

println(getCurrentShortDate())

输出是04-02-2015

于 2015-02-05T13:47:34.167 回答
5

斯威夫特 3

使用 @doovers 创建的扩展名和来自此网站的一些格式字符串,您将获得以下信息:

extension Date {
    func string(format: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

用法:

Date().string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017
Date().string(format: "MM/dd/yyyy")        // 10/21/2017
Date().string(format: "MM-dd-yyyy HH:mm")  // 10-21-2017 03:31

Date().string(format: "MMM d, h:mm a")     // Oct 21, 3:31 AM
Date().string(format: "MMMM yyyy")         // October 2017
Date().string(format: "MMM d, yyyy")       // Oct 21, 2017

Date().string(format: "E, d MMM yyyy HH:mm:ss Z") // Sat, 21 Oct 2017 03:31:40 +0000
Date().string(format: "yyyy-MM-dd'T'HH:mm:ssZ")   // 2017-10-21T03:31:40+0000
Date().string(format: "dd.MM.yy")                 // 21.10.17

您还可以将毫秒传递给 date 对象,如下所示:

Date(1508577868947).string(format: "EEEE, MMM d, yyyy") // Saturday, Oct 21, 2017
于 2017-10-21T09:30:47.757 回答
3

这是Apple建议的方式

    let formatter = DateFormatter()
    formatter.locale = Locale.current
    formatter.dateStyle = .short
    let dateString = formatter.string(from: date)

或者,如果 .short、.medium、.long 和 .full 的预定义格式非常正确,则在语言环境中创建模板

    let formatter = DateFormatter()
    formatter.locale = Locale.current
    formatter.setLocalizedDateFormatFromTemplate("MM/dd/yyyy")
    let dateString = formatter.string(from: date)

这是链接

于 2018-04-11T20:29:26.090 回答
3

这是更改日期格式的最简单方法。

1. 为 Date 扩展创建一个 swift 类。

extension Date {
    func today(format : String = "yyyy-MM-dd'T'HH:mm:ss.SSS") -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
} 
  1. 如何使用扩展。

    让日期 = Date().today(格式:“dd/MM/yy HH:mm:ss”)

    打印(日期)

于 2021-04-06T11:40:00.940 回答
2
 let dateformatter1 = DateFormatter()
    dateformatter1.dateFormat = "ccc, d MMM yyy"

    let dateString1 = dateformatter1.string(from: datePicker.date)
    print("Date Selected \(dateString1)")
    labelDate.text = dateString1

    let dateformatter2 = DateFormatter()
    dateformatter2.dateFormat = "dd-MM-yyyy"
    let dateString2 = dateformatter2.string(from: datePicker.date)
    print("Date Selected \(dateString2)")


    let dateformatter3 = DateFormatter()
    dateformatter3.dateFormat = "dd/MM/yyyy"
    let dateString3 = dateformatter3.string(from: datePicker.date)
    print("Date Selected \(dateString3)")

    let dateformatter4 = DateFormatter()
    dateformatter4.dateFormat = "dd MMMM yyyy hh:mm a"
    let dateString4 = dateformatter4.string(from: datePicker.date)
    print("Date Selected \(dateString4)") 

我在Codzify.com上提到了这篇文章。真的很有帮助。

于 2018-05-22T10:57:32.070 回答