Swift中只有一个日期(没有时间)的课程吗?(或基础班)

Gre*_*reg 5 date nsdate ios swift swift2

Swift中只有一个日期(没有时间)的课程吗?(或基础班)

(如果没有,但有一个众所周知/流行的开源库,如果你能提到这个,就实现这个)

编辑:更新以澄清"(或基础课程)"

Fre*_*ust 12

没有日期只有类是Foundation框架的一部分.

这是获取NSDate对象的仅日期表示的快速方法:

let now = NSDate()

let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

print(dateFormatter.stringFromDate(now)) // Mar 3, 2016
Run Code Online (Sandbox Code Playgroud)

NSDate总是有时间,因为日期是一个单一的时间点.如果您如此倾向,可以创建一个没有时间组件的日期,但通常默认为12AM:

let dateString = "2016-03-03"
let dateFromStringFormatter = NSDateFormatter()
dateFromStringFormatter.dateFormat = "YYYY-MM-dd"

let dateFromString = dateFromStringFormatter.dateFromString(dateString)
// dateFromString shows "Mar 3, 2016, 12:00 AM"
Run Code Online (Sandbox Code Playgroud)

适用于Swift 3.0+

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd"

// optional
let date = dateFormatter.date(from: "2016-03-03") // Mar 3, 2015 at 12:00 AM
Run Code Online (Sandbox Code Playgroud)


Jon*_*era 9

没有日期类Foundation,但您可以Date使用日历从对象中删除时间.在Swift 4中:

func stripTime(from originalDate: Date) -> Date {
    let components = Calendar.current.dateComponents([.year, .month, .day], from: originalDate)
    let date = Calendar.current.date(from: components)
    return date!
}
Run Code Online (Sandbox Code Playgroud)

或作为扩展:

extension Date {

    func stripTime() -> Date {
        let components = Calendar.current.dateComponents([.year, .month, .day], from: self)
        let date = Calendar.current.date(from: components)
        return date!
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 这不会真正剥离任何东西。它将仅返回另一个日期,时间设置为00:00(午夜)。您可以使用Calendar.current.startOfDay(for:date)实现相同的目的 (5认同)

Joh*_*rry 6

Swift 3.0

let date = Date()

let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.none
dateFormatter.dateStyle = DateFormatter.Style.short

dateFormatter.string(from: date) // 12/15/16
Run Code Online (Sandbox Code Playgroud)


Ral*_*ert 5

Swift 标准库或 Foundation 中没有仅日期(无时间)类型(从 iOS 13 开始)。

您可以使用以下类型CalendarDate在 Swift 中将日期表示为年、月、日:

也可通过此处的 Swift 包管理器获取:CalendarDate

import Foundation

/**
 CalendarDate is a Swift type that represents a Date as year, month and day value.
 Includes support for formatting as a ISO 8601 string ('yyyy-mm-dd') and JSON coding.
 */
public struct CalendarDate: Equatable, Hashable {

    public let year, month, day: Int

    public static var today: CalendarDate {
        CalendarDate(date: Date())
    }

    public init(year: Int, month: Int, day: Int) {
        self.year = year
        self.month = month
        self.day = day
    }

    public init(date: Date) {
        let calendar = Calendar.current
        self.year = calendar.component(.year, from: date)
        self.month = calendar.component(.month, from: date)
        self.day = calendar.component(.day, from: date)
    }

    public var date: Date {
        DateComponents(calendar: Calendar.current, year: self.year, month: self.month, day: self.day).date!
    }

}

extension CalendarDate: LosslessStringConvertible {

    public init?(_ description: String) {
        if let date = Self.formatter.date(from: description) {
            self.init(date: date)
        } else {
            return nil
        }
    }

    public var description: String {
        Self.formatter.string(from: self.date)
    }

    private static let formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
}

extension CalendarDate: Codable {

    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        let string = try container.decode(String.self)

        guard let value = CalendarDate(string) else {
            throw DecodingError.dataCorruptedError(
                in: container,
                debugDescription: "Not a valid calendar date: \"\(string)\""
            )
        }

        self = value
    }

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(self.description)
    }

}
Run Code Online (Sandbox Code Playgroud)