Swift 中是否只有日期(没有时间)class? (或基金会 classes)
Is there a date only (no time) class in Swift? (or Foundation classes)
Swift中是否只有日期(没有时间)class? (或基金会 classes)
(顺便说一句,如果没有,但有一个很好的 known/popular 开源库可以实现这个,如果你能提到这个的话)
编辑:更新以澄清“(或 Foundation classes)”
只有 class 没有日期是 Foundation 框架的一部分。
这是获取 NSDate 对象的仅日期表示的快速方法:
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
print(dateFormatter.stringFromDate(now)) // Mar 3, 2016
NSDate 总是有时间,因为日期是一个时间点。如果您愿意,可以创建一个没有时间部分的日期,但通常默认为凌晨 12 点:
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"
对于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
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
我会说像 "YYYY-MM-DD" + NSCalendar.Identifier 这样指定的日期值字符串可以表示一个日期。
有了这个,您就可以将这个日期转换为任何其他日历。
如果你想在Swift4中得到一个Date对象,比如当你试图在核心数据中存储一个Date对象时,你可以使用这个方法。
private func getDate() -> Date {
let today = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.none
dateFormatter .dateStyle = DateFormatter.Style.short
let dateAsString = dateFormatter.string(from: today)
return dateFormatter.date(from: dateAsString)!
}
基于@John R Perry 的回答。
Foundation
中没有日期 class,但您可以使用日历从 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!
}
或作为扩展:
extension Date {
func stripTime() -> Date {
let components = Calendar.current.dateComponents([.year, .month, .day], from: self)
let date = Calendar.current.date(from: components)
return date!
}
}
我知道已经有一些很好的答案了..只是想在这里传递这个,它打印 "true",所以如果你使用这些组件或者只是将时间设置为 00:00:00基本一样...
let startComp = Calendar.current.dateComponents([.year, .month, .day], from: Date())
let first = Calendar.current.date(from: startComp)!
print(first == Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()))
希望这对某人有所帮助:)
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)
}
}
Swift中是否只有日期(没有时间)class? (或基金会 classes)
(顺便说一句,如果没有,但有一个很好的 known/popular 开源库可以实现这个,如果你能提到这个的话)
编辑:更新以澄清“(或 Foundation classes)”
只有 class 没有日期是 Foundation 框架的一部分。
这是获取 NSDate 对象的仅日期表示的快速方法:
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
print(dateFormatter.stringFromDate(now)) // Mar 3, 2016
NSDate 总是有时间,因为日期是一个时间点。如果您愿意,可以创建一个没有时间部分的日期,但通常默认为凌晨 12 点:
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"
对于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
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
我会说像 "YYYY-MM-DD" + NSCalendar.Identifier 这样指定的日期值字符串可以表示一个日期。
有了这个,您就可以将这个日期转换为任何其他日历。
如果你想在Swift4中得到一个Date对象,比如当你试图在核心数据中存储一个Date对象时,你可以使用这个方法。
private func getDate() -> Date {
let today = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = DateFormatter.Style.none
dateFormatter .dateStyle = DateFormatter.Style.short
let dateAsString = dateFormatter.string(from: today)
return dateFormatter.date(from: dateAsString)!
}
基于@John R Perry 的回答。
Foundation
中没有日期 class,但您可以使用日历从 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!
}
或作为扩展:
extension Date {
func stripTime() -> Date {
let components = Calendar.current.dateComponents([.year, .month, .day], from: self)
let date = Calendar.current.date(from: components)
return date!
}
}
我知道已经有一些很好的答案了..只是想在这里传递这个,它打印 "true",所以如果你使用这些组件或者只是将时间设置为 00:00:00基本一样...
let startComp = Calendar.current.dateComponents([.year, .month, .day], from: Date())
let first = Calendar.current.date(from: startComp)!
print(first == Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()))
希望这对某人有所帮助:)
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)
}
}