{"id":15033413,"url":"https://github.com/malcommac/swiftdate","last_synced_at":"2025-05-13T19:03:01.812Z","repository":{"id":31578940,"uuid":"35143711","full_name":"malcommac/SwiftDate","owner":"malcommac","description":"🐔 Toolkit to parse, validate, manipulate, compare and display dates, time \u0026 timezones in Swift.","archived":false,"fork":false,"pushed_at":"2023-09-19T09:59:48.000Z","size":27837,"stargazers_count":7681,"open_issues_count":93,"forks_count":772,"subscribers_count":115,"default_branch":"master","last_synced_at":"2025-05-13T03:34:37.367Z","etag":null,"topics":["date","date-formatting","date-time","nsdate","nsdate-category","swift","swiftdate","timezone"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/malcommac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"custom":"https://www.paypal.com/paypalme2/danielemargutti"}},"created_at":"2015-05-06T06:46:24.000Z","updated_at":"2025-05-12T15:59:30.000Z","dependencies_parsed_at":"2024-01-29T17:08:06.071Z","dependency_job_id":null,"html_url":"https://github.com/malcommac/SwiftDate","commit_stats":{"total_commits":924,"total_committers":138,"mean_commits":6.695652173913044,"dds":0.4285714285714286,"last_synced_commit":"5d943224c3bb173e6ecf27295611615eba90c80e"},"previous_names":[],"tags_count":73,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FSwiftDate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FSwiftDate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FSwiftDate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malcommac%2FSwiftDate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/malcommac","download_url":"https://codeload.github.com/malcommac/SwiftDate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254010793,"owners_count":21998993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["date","date-formatting","date-time","nsdate","nsdate-category","swift","swiftdate","timezone"],"created_at":"2024-09-24T20:21:09.706Z","updated_at":"2025-05-13T19:03:01.752Z","avatar_url":"https://github.com/malcommac.png","language":"Swift","funding_links":["https://www.paypal.com/paypalme2/danielemargutti"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\" \u003e\n  \u003cimg src=\"banner.png\" width=300px alt=\"SwiftDate\" title=\"SwiftDate\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\u003cstrong\u003eToolkit to parse, validate, manipulate, compare and display dates, time \u0026 timezones in Swift.\u003c/strong\u003e\u003c/p\u003e\n\n## What's This?\n\nSwiftDate is the **definitive toolchain to manipulate and display dates and time zones** on all Apple platform and even on Linux and Swift Server Side frameworks like Vapor or Kitura.  \n**Over 3 million of downloads on [CocoaPods](https://cocoapods.org/pods/SwiftDate).**\n\nFrom simple date manipulation to complex business logic SwiftDate maybe the right choice for your next project.\n\n- [x] **Easy Date Parsing** (custom formats, iso8601, rss \u0026 many more)\n- [x] **Easy Date Formatting** even with colloquial formatter and 140+ supported languages\n- [x] **Easy math operations with time units** (`2.hours + 5.minutes`...)\n- [x] **Intuitive components extraction** (`day, hour, nearestHour, weekdayNameShort` etc.)\n- [x] **Derivated dates generation** (`nextWeek, nextMonth, nextWeekday, tomorrow`...)\n- [x] Over **20+ fine grained date comparison** functions (`isToday, isTomorrow, isSameWeek, isNextYear`...)\n- [x] Swift 4's **Codable Support**\n- [x] **Random dates** generation\n- [x] **Fine grained date enumeration** functions\n- [x] **Time period** support\n- [x] **Convert TimeIntervals** to other units (`2.hours.toUnits(.minutes)`)\n\nand of course...\n\n- **IT'S TESTED!**. As 5.x the project has 90% of code coverage (want help us? write some unit tests and make a PR)\n- **IT'S FULLY DOCUMENTED!**, [both with a complete guide](/Documentation/Index.md) and with Jazzy!\n- **WE LOVE PLAYGROUND!** [Check out](/Playgrounds/SwiftDate.playground) our interactive playground!\n\n\n## Start with SwiftDate\n\nThe entire library is fully documented both via XCode method inspector and a complete markdown documentation you can found below.\n\n- → **[Full Documentation](/Documentation/Index.md)**\n- → **[Requirements, Install, License \u0026 More](/Documentation/0.Informations.md)**\n- → **[Upgrading from SwiftDate 4](/Documentation/10.Upgrading_SwiftDate4.md)**\n\n### Explore SwiftDate\n\nFrom simple date manipulation to complex business logic SwiftDate maybe the right choice for your next project.\n\nLet me show to you the main features of the library:\n\n- [Date Parsing](#1)\n- [Date Manipulation](#2)\n- [Date Comparsion](#3)\n- [Date Creation with Region (Timezone, Calendar \u0026 Locale)](#4)\n- [Derivated Dates](#5)\n- [Components Extraction](#6)\n- [Switch between timezones/locale and calendars](#7)\n- [Date Formatting](#8)\n- [Relative Date Formatting (fully customizable!)](#9)\n- [Codable Support](#10)\n- [Time Periods](#11)\n\n\u003ca name=\"1\"/\u003e\n\n### 1. Date Parsing\nSwiftDate can recognize all the major datetime formats  automatically (ISO8601, RSS, Alt RSS, .NET, SQL, HTTP...) and you can also provide your own formats.\nCreating a new date has never been so easy!\n\n```swift\n// All default datetime formats (15+) are recognized automatically\nlet _ = \"2010-05-20 15:30:00\".toDate()\n// You can also provide your own format!\nlet _ = \"2010-05-20 15:30\".toDate(\"yyyy-MM-dd HH:mm\")\n// All ISO8601 variants are supported too with timezone parsing!\nlet _ = \"2017-09-17T11:59:29+02:00\".toISODate()\n// RSS, Extended, HTTP, SQL, .NET and all the major variants are supported!\nlet _ = \"19 Nov 2015 22:20:40 +0100\".toRSS(alt: true)\n\n```\n\n\u003ca name=\"2\"/\u003e\n\n### 2. Date Manipulation\nDate can be manipulated by adding or removing time components using a natural language; time unit extraction is also easy and includes the support for timezone, calendar and locales!\n\nManipulation can be done with standard math operators and between dates, time intervals, date components and relevant time units!\n\n```swift\n// Math operations support time units\nlet _ = (\"2010-05-20 15:30:00\".toDate() + 3.months - 2.days)\nlet _ = Date() + 3.hours\nlet _ = date1 + [.year:1, .month:2, .hour:5]\nlet _ = date1 + date2\n// extract single time unit components from date manipulation\nlet over1Year = (date3 - date2).year \u003e 1\n```\n\u003ca name=\"3\"/\u003e\n\n### 3. Date Comparison\nSwiftDate include an extensive set of comparison functions; you can compare two dates by granularity, check if a date is an particular day, range and practically any other comparison you ever need.\n\nComparison is also available via standard math operators like (`\u003e, \u003e=, \u003c, \u003c=`).\n\n```swift\n// Standard math comparison is allowed\nlet _ = dateA \u003e= dateB || dateC \u003c dateB\n\n// Complex comparisons includes granularity support\nlet _ = dateA.compare(toDate: dateB, granularity: .hour) == .orderedSame\nlet _ = dateA.isAfterDate(dateB, orEqual: true, granularity: .month) // \u003e until month granularity\nlet _ = dateC.isInRange(date: dateA, and: dateB, orEqual: true, granularity: .day) // \u003e until day granularity\nlet _ = dateA.earlierDate(dateB) // earlier date\nlet _ = dateA.laterDate(dateB) // later date\n\n// Check if date is close to another with a given precision\nlet _ = dateA.compareCloseTo(dateB, precision: 1.hours.timeInterval\n\n// Compare for relevant events:\n// .isToday, .isYesterday, .isTomorrow, .isWeekend, isNextWeek\n// .isSameDay, .isMorning, .isWeekday ...\nlet _ = date.compare(.isToday)\nlet _ = date.compare(.isNight)\nlet _ = date.compare(.isNextWeek)\nlet _ = date.compare(.isThisMonth)\nlet _ = date.compare(.startOfWeek)\nlet _ = date.compare(.isNextYear)\n// ...and MORE THAN 30 OTHER COMPARISONS BUILT IN\n\n// Operation in arrays (oldestIn, newestIn, sortedByNewest, sortedByOldest...)\nlet _ = DateInRegion.oldestIn(list: datesArray)\nlet _ = DateInRegion.sortedByNewest(list: datesArray)\n```\n\n\u003ca name=\"4\"/\u003e\n\n### 4. Date Creation with Region (Timezone, Calendar \u0026 Locale)\nYou can create new dates from a string, time intervals or using date components. SwiftDate offers a wide set of functions to create and derivate your dates even with random generation!\n\n```swift\n// All dates includes timezone, calendar and locales!\n// Create from string\nlet rome = Region(calendar: Calendars.gregorian, zone: Zones.europeRome, locale: Locales.italian)\nlet date1 = DateInRegion(\"2010-01-01 00:00:00\", region: rome)!\n\n// Create date from intervals\nlet _ = DateInRegion(seconds: 39940, region: rome)\nlet _ = DateInRegion(milliseconds: 5000, region: rome)\n\n// Date from components\nlet _ = DateInRegion(components: {\n\t$0.year = 2001\n\t$0.month = 9\n\t$0.day = 11\n\t$0.hour = 12\n\t$0.minute = 0\n}, region: rome)\nlet _ = DateInRegion(year: 2001, month: 1, day: 5, hour: 23, minute: 30, second: 0, region: rome)\n\n// Random date generation with/without bounds\nlet _ = DateInRegion.randomDate(region: rome)\nlet _ = DateInRegion.randomDate(withinDaysBeforeToday: 5)\nlet _ = DateInRegion.randomDates(count: 50, between: lowerLimitDate, and: upperLimitDate, region: rome)\n```\n\u003ca name=\"5\"/\u003e\n\n### 5. Derivated Dates\nDate can be also generated starting from other dates; SwiftDate includes an extensive set of functions to generate.\nOver 20 different derivated dates can be created easily using `dateAt()` function.\n\n```swift\nlet _ = DateInRegion().dateAt(.endOfDay) // today at the end of the day\n// Over 20 different relevant dates including .startOfDay,\n// .endOfDay, .startOfWeek, .tomorrow, .nextWeekday, .nextMonth, .prevYear, .nearestMinute and many others!\nlet _ = dateA.nextWeekday(.friday) // the next friday after dateA\nlet _ = (date.dateAt(.startOfMonth) - 3.days)\nlet _ = dateA.compare(.endOfWeek)\n\n// Enumerate dates in range by providing your own custom\n// increment expressed in date components\nlet from = DateInRegion(\"2015-01-01 10:00:00\", region: rome)!\nlet to = DateInRegion(\"2015-01-02 03:00:00\", region: rome)!\nlet increment2 = DateComponents.create {\n\t$0.hour = 1\n\t$0.minute = 30\n\t$0.second = 10\n}\n// generate dates in range by incrementing +1h,30m,10s each new date\nlet dates = DateInRegion.enumerateDates(from: fromDate2, to: toDate2, increment: increment2)\n\n// Get all mondays in Jan 2019\nlet mondaysInJan2019 = Date.datesForWeekday(.monday, inMonth: 1, ofYear: 2019)\n\n// Altering time components\nlet _ = dateA.dateBySet(hour: 10, min: 0, secs: 0)\n\n// Truncating a date\nlet _ = dateA.dateTruncated(at: [.year,.month,.day]) // reset all time components keeping only date\n\n// Rounding a date\nlet _ = dateA.dateRoundedAt(.toMins(10))\nlet _ = dateA.dateRoundedAt(.toFloor30Mins)\n\n// Adding components\nlet _ = dateA.dateByAdding(5,.year)\n\n// Date at the start/end of any time component\nlet _ = dateA.dateAtEndOf(.year) // 31 of Dec at 23:59:59\nlet _ = dateA.dateAtStartOf(.day) // at 00:00:00 of the same day\nlet _ = dateA.dateAtStartOf(.month) // at 00:00:00 of the first day of the month\n```\n\n\u003ca name=\"6\"/\u003e\n\n### 6. Components Extraction\nYou can extract components directly from dates and it includes the right value expressed in date's region (the right timezone and set locale!).\n\n```swift\n// Create a date in a region, London but with the lcoale set to IT\nlet london = Region(calendar: .gregorian, zone: .europeLondon, locale: .italian)\nlet date = DateInRegion(\"2018-02-05 23:14:45\", format: dateFormat, region: london)!\n\n// You can extract any of the all available time units.\n// VALUES ARE EXPRESSED IN THE REGION OF THE DATE (THE RIGHT TIMEZONE).\n// (you can still get the UTC/absolute value by getting the inner's absoluteDate).\n\nlet _ = date.year // 2018\nlet _ = date.month // 2\nlet _ = date.monthNameDefault // 'Febbraio' as the locale is the to IT!\nlet _ = date.firstDayOfWeek // 5\nlet _ = date.weekdayNameShort // 'Lun' as locale is the to IT\n// ... all components are supported: .year, .month, .day, .hour, .minute, .second,\n// .monthName, .weekday, .nearestHour, .firstDayOfWeek. .quarter and so on...\n```\n\n\u003ca name=\"7\"/\u003e\n\n### 7. Switch between timezones/locale and calendars\nYou can easily convert any date to another region (aka another calendar, locale or timezone) easily!\nNew date contains all values expressed into the destination reason\n\n```swift\n// Conversion between timezones is easy using convertTo(region:) function\nlet rNY = Region(calendar: Calendars.gregorian, zone: Zones.americaNewYork, locale: Locales.english)\nlet rRome = Region(calendar: Calendars.gregorian, zone: Zones.europeRome, locale: Locales.italian)\nlet dateInNY = \"2017-01-01 00:00:00\".toDate(region: rNY)\nlet dateInRome = dateInNY?.convertTo(region: rRome)!\nprint(dateInRome.toString()) // \"dom gen 01 06:00:00 +0100 2017\\n\"\n\n// You can also convert single region's attributes\nlet dateInIndia = dateInNY?.convertTo(timezone: Zones.indianChristmas, locale: Locales.nepaliIndia)\nprint(\"\\(dateInIndia!.toString())\") // \"आइत जनवरी ०१ १२:००:०० +0700 २०१७\\n\"\n```\n\n\u003ca name=\"8\"/\u003e\n\n### 8. Date Formatting\nDate formatting is easy, you can specify your own format, locale or use any of the provided ones.\n\n```swift\n// Date Formatting\nlet london = Region(calendar: .gregorian, zone: .europeLondon, locale: .english)\nlet date = ... // 2017-07-22T18:27:02+02:00 in london region\nlet _ = date.toDotNET() // /Date(1500740822000+0200)/\nlet _ = date.toISODate() // 2017-07-22T18:27:02+02:00\nlet _ = date.toFormat(\"dd MMM yyyy 'at' HH:mm\") // \"22 July 2017 at 18:27\"\n\n// You can also easily change locale when formatting a region\nlet _ = date.toFormat(\"dd MMM\", locale: .italian) // \"22 Luglio\"\n\n// Time Interval Formatting as Countdown\nlet interval: TimeInterval = (2.hours.timeInterval) + (34.minutes.timeInterval) + (5.seconds.timeInterval)\nlet _ = interval.toClock() // \"2:34:05\"\n\n// Time Interval Formatting by Components\nlet _ = interval.toString {\n\t$0.maximumUnitCount = 4\n\t$0.allowedUnits = [.day, .hour, .minute]\n\t$0.collapsesLargestUnit = true\n\t$0.unitsStyle = .abbreviated\n} // \"2h 34m\"\n```\n\n\u003ca name=\"9\"/\u003e\n\n### 9. Relative Date Formatting (fully customizable!)\nRelative formatting is all new in SwiftDate; it supports 120+ languages with two different styles (`.default, .twitter`), 9 flavours (`.long, .longTime, .longConvenient, .short, .shortTime, .shortConvenient, .narrow, .tiny, .quantify`) and all of them are customizable as you need.\nThe extensible format allows you to provide your own translations and rules to override the default behaviour.\n\n```swift\n// Twitter Style\nlet _ = (Date() - 3.minutes).toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.english) // \"3m\"\nlet _ = (Date() - 6.minutes).toRelative(style: RelativeFormatter.twitterStyle(), locale: Locales.italian) // \"6 min fa\"\n\n// Default Style\nlet _ = (now2 - 5.hours).toRelative(style: RelativeFormatter.defaultStyle(), locale: Locales.english) // \"5 hours ago\"\nlet y = (now2 - 40.minutes).toRelative(style: RelativeFormatter.defaultStyle(), locale: Locales.italian) // \"45 minuti fa\"\n```\n\u003ca name=\"10\"/\u003e\n\n### 10. Codable Support\nBoth `DateInRegion` and `Region` fully support the new Swift's `Codable` protocol. This mean you can safely encode/decode them:\n\n```swift\n// Encoding/Decoding a Region\nlet region = Region(calendar: Calendars.gregorian, zone: Zones.europeOslo, locale: Locales.english)\nlet encodedJSON = try JSONEncoder().encode(region)\nlet decodedRegion = try JSONDecoder().decode(Region.self, from: encodedJSON)\n\n// Encoding/Decoding a DateInRegion\nlet date = DateInRegion(\"2015-09-24T13:20:55\", region: region)\nlet encodedDate = try JSONEncoder().encode(date)\nlet decodedDate = try JSONDecoder().decode(DateInRegion.self, from: encodedDate)\n```\n\n\u003ca name=\"11\"/\u003e\n\n### 11. Time Periods\nSwiftDate integrates the great Matthew York's [DateTools](https://github.com/MatthewYork/DateTools) module in order to support Time Periods.\n\nSee [Time Periods](/Documentation/12.Timer_Periods.md) section of the documentation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalcommac%2Fswiftdate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmalcommac%2Fswiftdate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalcommac%2Fswiftdate/lists"}