VDDate
Robust set of extensions for the Date struct
Install / Use
/learn @dankinsoid/VDDateREADME
VDDate 📆
VDDate is a Swift library that offers a robust set of extensions for the Date struct, enhancing its capabilities and making date manipulation in Swift more intuitive and powerful.
Features
Date
- Convenience
Date.inits with components parameters year, month, day etc. - Convenience
Date.inits from string with format. - Static computed property
Date.nowthat can be mocked if needed. It's recommended to useDate.nowinstead ofDate(). - Helper vars:
iToday,isTomorrow,isYesterday,iso8601. - var
components, var for each component:day,month,yearetc, mutating subscript withCalendar.Component. start(of: Calendar.Component) -> Date.end(of: Calendar.Component) -> Date.matches(DateComponents) -> Bool.isInSame(Calendar.Component) -> Bool.isCurrent(Calendar.Component) -> BoolisEqual(to: Date, toGranularity: Calendar.Component) -> Bool.number(of: Calendar.Component, from: Date) -> Intnumbers(of components: Set<Calendar.Component>, from: Date) -> DateComponentscomponents(_ components: Set<Calendar.Component>, from: Date) -> DateComponentsrange(of: Calendar.Component, in: Calendar.Component) -> Range<Int>- range of component, for examplerange(of: .day, in: .month)returns smth like1..<32.interval(byAdding: DateComponents) -> DateIntervalinterval(of: Calendar.Component, in: Calendar.Component) -> DateIntervalnumber(of: Calendar.Component) -> Intnumber(of: Calendar.Component, in: Calendar.Component) -> IntDateFormatstruct with predefined format components.string(DateFormat) -> StringNote: Deprecated since iOS 15.0 in favor offormatted(FormatStyle).string(date: DateFormatter.Style, time: DateFormatter.Style) -> StringNote: Deprecated since iOS 15.0 in favor offormatted(FormatStyle).string(format: RelativeDateFormat<DateFormat>)- method for converting formatting date relative to current (or any other) date. Note: Deprecated since iOS 15.0 in favor offormatted(RelativeDateFormatStyle)formatted(RelativeDateFormatStyle)name(of: Calendar.Component) -> Stringordinality(of: Calendar.Component, in: Calendar.Component) -> Int?and convenience methods for each component likeday(in: Calendar.Component).- Minus operator for date returns TimeInterval.
adding(DateComponents) -> Dateadding(Int, Calendar.Component) -> Datesetting(DateComponents) -> Datesetting(Int, Calendar.Component) -> Datecompare(with: Date, toGranularity: Calendar.Component) -> ComparasionResultnextWeekend(direction: Calendar.SearchDirection) -> DateInterval?next(Calendar.Component, direction: Calendar.SearchDirection) -> Datenearest(DateComponents) -> Date?rounded(Calendar.Component, by: Int) -> Datedate rounded by some component, useful when you deal with regular time intervals.
Note
All methods accept a Calendar parameter and, in some cases, TimeZone or Locale.
I've introduced static variables: Calendar.default, TimeZone.default, and Locale.default.
These variables serve as the default values for each respective method. You can modify each default variable either globally or specifically for a given method.
Overload for Calendar parameter globally:
Calendar.bootstrap(default: Calendar(identifier: .gregorian))
Use Calendar parameter for a specific method:
date.start(of: .day, calendar: Calendar(identifier: .gregorian))
Tip: You can use TaskLocal to set the default value for a specific task and it's subtasks.
extension Calendar {
@TaskLocal
static var local: Calendar = .autoupdatingCurrent
}
Calendar.bootstrap(default: .local)
Calendar.$local.withValue(Calendar(identifier: .gregorian)) {
print(Date.now.start(of: .day))
}
TimeInterval
DatesCollectionstruct and functioneach(Int, Calendar.Component) -> DatesCollection.
DateComponents
- Arithmetic operators: +, - between
DateComponents, +, -, /, * betweenDateComponentsandInt, +, - betweenDateComponentsandDate. - Convenience static methods for each component like
.day(3). - Extensions on
BinaryIntegerlike2.days, so now it possible to writedate + 2.days. DateComponentsnow is expressible by dictionary literal like[.day: 2, .month: 1]- mutating subscript with
Calendar.Components. rawValue: [Calendar.Component: Int]- dictionary with components.
Installation
Create a Package.swift file.
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDDate.git", from: "0.14.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDDate"])
]
)
$ swift build
Author
dankinsoid, voidilov@gmail.com
License
VDDate is available under the MIT license. See the LICENSE file for more info.
Related Skills
node-connect
347.6kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.6kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.6kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
