Introduction

May 30, 2026 · View on GitHub

NVDate is a Swift Date utility library, created to make date and time manipulation easier.

Release Build Coverage

Features

  • A lot of API functions to make date and time manipulation easier and fun
  • Self-explanatory & intuitive naming convention
  • NVDate functions are chainable
  • Easy to use
  • Open Source!

Installation

◉ Using Swift Package Manager

Add this package URL in Xcode or your Package.swift:

.package(url: "https://github.com/novalagung/NVDate.git", from: "3.0.0")

Then add NVDate as a dependency of your target and import it:

import NVDate

◉ Using CocoaPods

Swift Package Manager is the recommended installation path for NVDate 3.0.0 and newer. CocoaPods support is kept for existing projects.

Add these into your Podfile:

pod 'NVDate', :git => 'https://github.com/novalagung/NVDate.git', :tag => '3.0.0'

Then import NVDate into your Swift file.

import NVDate
Legacy Objective-C

Add these into your Podfile:

pod 'NVDate', :git => 'https://github.com/novalagung/NVDate.git', :tag => '1.0.1'

Then import NVdate.h into your .h or .m file.

#import "NVDate.h"

Example

◉ Today date as string
let date = NVDate()

print(date.asString())
// ==> Wednesday, February 5, 2014, 4:56:35 PM Western Indonesia Time
◉ Date 2018/05/25 as string
let date = NVDate(fromString: "2018/05/25", withFormat: "yyyy/MM/dd")

print(date.asString())
// ==> Friday, May 25, 2018, 00:00:00 AM Western Indonesia Time
◉ Last day of next 2 months
let date = NVDate()
    .nextMonths(diff: 2)
    .lastDayOfMonth()

print(date.asString(withFormat: "dd-MM-yyyy"))
// ==> 30-04-2014
◉ Second week of 2 months ago
let date = NVDate()
    .previousMonths(diff: 2)
    .firstDayOfMonth()
    .nextWeek()
date.dateFormat(setFormat: "yyyy-MM-dd HH:mm:ss")

print(date.asString())
// ==> 2013-12-08 17:03:36
◉ Detect if 2018/05/25 is friday
let todayIsFriday = NVDate(year: 2018, month: 5, day: 25)
    .previousDay()
    .isTodayName(.friday)

print(todayIsFriday)
// ==> false
◉ Dot syntax
let someday = NVDate()
    .previousDay()
    .previousWeek()
    .nextDay()
    .asString()

print(someday)
// ==> 2013-12-08 17:03:36

API Documentation

◉ Types

TypeValues
NVDate.DayName.sunday, .monday, .tuesday, .wednesday, .thursday, .friday, .saturday
NVDate.MonthName.january, .february, .march, .april, .may, .june, .july, .august, .september, .october, .november, .december

◉ Initializers

InitializerDescription
NVDate()Constructs an instance using the current date and time.
NVDate(fromString: String, withFormat: String)Constructs an instance by parsing a string with an explicit date format.
NVDate(year: Int, month: Int, day: Int)Constructs an instance from year, month, and day components.
NVDate(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int)Constructs an instance from full date and time components.
NVDate(fromDate: Date)Constructs an instance from an existing Date.
NVDate(fromTimeIntervalSinceReferenceDate: TimeInterval)Constructs an instance from a Foundation reference-date time interval.

◉ Formatting and Configuration

MethodReturnDescription
date()Date?Returns the underlying date value.
asString()StringFormats the date using the configured formatter.
asString(withFormat: String)StringFormats the date using the provided date format.
dateFormat()StringReturns the formatter's current date format.
dateFormat(setFormat: String)VoidSets the formatter's date format.
dateStyle()DateFormatter.StyleReturns the formatter's date style.
dateStyle(setStyle: DateFormatter.Style)VoidSets the formatter's date style.
timeStyle()DateFormatter.StyleReturns the formatter's time style.
timeStyle(setStyle: DateFormatter.Style)VoidSets the formatter's time style.
timeZone()TimeZoneReturns the configured time zone.
timeZone(setTimeZone: TimeZone)VoidSets the time zone used by the formatter and calendar.

◉ Date Arithmetic

MethodReturnDescription
setTimeAsZero()NVDateSets hour, minute, and second to 0.
nextDays(days: Int)NVDateMoves the date forward by the provided number of days.
nextDay()NVDateMoves the date forward by one day.
tomorrow()NVDateAlias of nextDay().
previousDays(diff: Int)NVDateMoves the date backward by the provided number of days.
previousDay()NVDateMoves the date backward by one day.
yesterday()NVDateAlias of previousDay().
nextWeeks(diff: Int)NVDateMoves the date forward by the provided number of weeks.
nextWeek()NVDateMoves the date forward by one week.
previousWeeks(diff: Int)NVDateMoves the date backward by the provided number of weeks.
previousWeek()NVDateMoves the date backward by one week.
nextMonths(diff: Int)NVDateMoves the date forward by the provided number of months.
nextMonth()NVDateMoves the date forward by one month.
previousMonths(diff: Int)NVDateMoves the date backward by the provided number of months.
previousMonth()NVDateMoves the date backward by one month.
nextYears(diff: Int)NVDateMoves the date forward by the provided number of years.
nextYear()NVDateMoves the date forward by one year.
previousYears(diff: Int)NVDateMoves the date backward by the provided number of years.
previousYear()NVDateMoves the date backward by one year.
firstDayOfMonth()NVDateMoves the date to the first day of its current month.
lastDayOfMonth()NVDateMoves the date to the last day of its current month.
firstMonthOfYear()NVDateMoves the date to January of its current year.
lastMonthOfYear()NVDateMoves the date to December of its current year.
nearestPreviousDay(_ dayName: NVDate.DayName)NVDateMoves the date to the nearest previous matching weekday.
nearestNextDay(_ dayName: NVDate.DayName)NVDateMoves the date to the nearest next matching weekday.

◉ Date Information

MethodReturnDescription
thisDayName()NVDate.DayNameReturns the weekday name for the current date value.
todayName()NVDate.DayNameAlias of thisDayName().
isThisDayName(_ dayName: NVDate.DayName)BoolReturns whether the date's weekday matches the provided weekday.
isTodayName(_ dayName: NVDate.DayName)BoolAlias of isThisDayName(_:).
thisMonthName()NVDate.MonthNameReturns the month name for the current date value.
isThisMonthName(_ monthName: NVDate.MonthName)BoolReturns whether the date's month matches the provided month.
year()IntReturns the year component.
year(setYear: Int)VoidSets the year component.
month()IntReturns the month component.
month(setMonth: Int)VoidSets the month component.
weekOfYear()IntReturns the week-of-year component.
weekOfMonth()IntReturns the week-of-month component.
day()IntReturns the day component.
day(setDay: Int)VoidSets the day component.
hour()IntReturns the hour component.
hour(setHour: Int)VoidSets the hour component.
minute()IntReturns the minute component.
minute(setMinute: Int)VoidSets the minute component.
second()IntReturns the second component.
second(setSecond: Int)VoidSets the second component.

Contribution

Feel free to contribute by doing fork -> pull request

License

http://novalagung.mit-license.org/