CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

Dates 1.0.1

Dates 1.0.1

Maintained by Lajos Deme Dr.



Dates 1.0.1

dates

Dates is a small Swift library containing useful Date extensions and functions that will speed up your development workflow.

Features

  • Makes your life a lot easier when you have to work with dates.
  • Dates is a lightweight library. You can breeze through the files over coffee.

Usage

Working with days:

let date = Date()

date.startOfDay // 2020-01-22 00:00:00
date.endOfDay // 2020-01-22 23:59:59

date.isWeekend // false

date.noon // 2020-01-22 12:00:00

date.tomorrow // 2020-01-23 12:00:00
date.yesterday // 2020-01-21 12:00:00

date.hoursLeftFromDay // 5
date.minutesLeftFromDay // 345
date.secondsLeftFromDay // 20755

Working with weeks:

let date = Date()

date.startOfWeek // 2020-01-20 00:00:00
date.endOfWeek // 2020-01-26 23:59:59
date.daysLeftFromWeek // 4
date.hoursLeftFromWeek // 101
date.minutesLeftFromWeek // 6095
date.secondsLeftFromWeek // 365722

Working with months:

let date = Date()

date.startOfMonth // 2020-01-01 00:00:00
date.endOfMonth // 2020-01-31 23:59:59

date.currentMonth // 1
date.nextMonth // 2
date.lastMonth // 12

date.month(byAdding: 3) // 4

date.weeksInMonth // 5
date.daysInMonth // 31
date.hoursInMonth // 743
date.minutesInMonth // 44639
date.secondsInMonth // 2678399

date.weeksInGivenMonth(month: 1) // 5
date.daysInGivenMonth(month: 1) // 31

numberOfGivenDayInMonth(selectedDay: .Wednesday) // 4

date.workdaysInMonth() // 23
date.weekendDaysInMonth() // 8

date.daysLeftFromMonth() // 9
date.hoursLeftFromMonth() // 220
date.minutesLeftFromMonth() // 44639
date.secondsLeftFromMonth() // 2678399

Working with years:

let date = Date()

date.startOfYear // 2020-01-01 00:00:00
date.endOfYear // 2020-12-31 23:59:59

date.currentYear // 2020
date.lastYear // 2019
date.nextYear // 2021

date.year(byAdding: 3) // 2023

date.weeksInYear // 53
date.daysInYear // 365
date.hoursInYear // 8783
date.minutesInYear // 527039
date.secondsInYear // 31622399

date.numberOfGivenDayInYear(selectedDay: .Friday) // 53

date.workdaysInYear() // 262
date.weekendDaysInYear() // 104

Getting local date:

Returns the date represented in the user's time zone.

let date = Date().localDate()

Formatting:

Creating a string that can be displayed with a timer:

let time: TimeInterval = 7209
time.timerFormat() // 02:00:09

Format dates however you want:

let date = Date()

date.format(dateStyle: .medium, timeStyle: .full, locale: Locale.current, timeZone: TimeZone.current) // Jan 22, 2020 at 8:05:32 PM Central European Standard Time

date.format(dateFormat: "E, d MMM yyyy HH:mm:ss Z", locale: Locale.current, timeZone: TimeZone.current) // Wed, 22 Jan 2020 20:05:32 +0100

Modifying:

let date = Date()

date.dateByAdding(month: 3, day: -2, hour: 1) // 2020-04-20 20:05:32

Comparing:

let date = Date()

date.isEqual(to: date.dateByAdding(year: 3, month: 1, day: -10)) // false

date.isEariler(than: date.dateByAdding(year: 3, month: 1, day: -10)) // true

date.isLater(than: Date().dateByAdding(year: 3, month: 1, day: -10)) // false

Measuring distance between dates:

let date = Date()

date.distance(to: date.dateByAdding(hour: 10), in: .minutes) // 600

Other variables and functions:

To access these you have to create a Dates() object.

let date = Date()
let dates = Dates()

dates.workdaysBetweenDates(from: date, to: date.dateByAdding(month: 3, day: -2, hour: 1)) // 64

dates.weekendDaysBetweenDates(from: date, to: date.dateByAdding(month: 3, day: -2, hour: 1)) // 26

//Handy variables
dates.hoursInDay // 24
dates.minutesInDay // 1440
dates.secondsInDay // 86400
dates.millisecondsInDay // 86400000

dates.daysInWeek // 7
dates.hoursInWeek // 168
dates.minutesInWeek // 10080
dates.secondsInWeek // 604800

dates.monthsInYear // 12
dates.weeksInYear // 54

Installation

CocoaPods

If you're using CocoaPods, just add this line to your Podfile:

pod 'Dates'

Install by running this command in your Terminal:

pod install

Then import the library where needed:

import Dates

Carthage:

Just add this to your Cartfile:

github "lajosdeme/Dates"

Contributing

If you have comments, complaints or ideas for improvements, feel free to open an issue or a pull request.

Author and license

Lajos Deme Dr.

Dates is available under the MIT license. See the LICENSE file for more info.