Skip to content

Instantly share code, notes, and snippets.

@sergii-frost
Created June 13, 2017 14:07
Show Gist options
  • Save sergii-frost/2a8a73c63be81eb537b7264331b7fa4f to your computer and use it in GitHub Desktop.
Save sergii-frost/2a8a73c63be81eb537b7264331b7fa4f to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
//
// NSDate+Compare.swift
// Node
//
// Created by Sergii Frost on 2017-06-13.
// Copyright © 2017 Rebtel. All rights reserved.
//
import Foundation
public extension Date {
/**
* Returns a true if receiver is equal to provided comparison date, otherwise returns false
*
* - parameter date: Provided date for comparison
*
* - returns: Bool representing comparison result
*/
public func equals(_ date: Date?) -> Bool {
guard let date = date else {
return false
}
return self.compare(date) == .orderedSame
}
/**
* Returns whether two dates fall on the same day.
*
* - parameter date: Date to compare with sender
*
* - returns: True if both paramter dates fall on the same day, false otherwise
*/
public func isSameDay(date : Date?) -> Bool {
guard let date = date else {
return false
}
return Date.isSameDay(date: self, as: date)
}
/**
* Returns whether two dates fall on the same month.
*
* - parameter date: Date to compare with sender
*
* - returns: True if both paramter dates fall on the same month, false otherwise
*/
public func isSameMonth(date : Date?) -> Bool {
guard let date = date else {
return false
}
return Date.isSameMonth(date: self, as: date)
}
/**
* Returns whether two dates fall on the same day.
*
* - parameter date: First date to compare
* - parameter compareDate: Second date to compare
*
* - returns: True if both paramter dates fall on the same day, false otherwise
*/
public static func isSameDay(date: Date, as compareDate: Date) -> Bool {
var calendar = Calendar.autoupdatingCurrent
calendar.timeZone = TimeZone.autoupdatingCurrent
var components = calendar.dateComponents([.era, .year, .month, .day], from: date)
let dateOne = calendar.date(from: components)
print(components)
components = calendar.dateComponents([.era, .year, .month, .day], from: compareDate)
print(components)
let dateTwo = calendar.date(from: components)
return dateOne?.equals(dateTwo) ?? false
}
/**
* Returns whether two dates fall on the same month.
*
* - parameter date: First date to compare
* - parameter compareDate: Second date to compare
*
* - returns: True if both paramter dates fall on the same month, false otherwise
*/
public static func isSameMonth(date: Date, as compareDate: Date) -> Bool {
let calendar = Calendar.autoupdatingCurrent
var components = calendar.dateComponents([.era, .year, .month], from: date)
let dateOne = calendar.date(from: components)
components = calendar.dateComponents([.era, .year, .month], from: compareDate)
let dateTwo = calendar.date(from: components)
return dateOne?.equals(dateTwo) ?? false
}
}
public extension NSDate {
/**
* Returns whether two dates fall on the same day.
*
* - parameter date: NSDate to compare with sender
*
* - returns: True if both paramter dates fall on the same day, false otherwise
*/
public func isSameDay(date: NSDate?) -> Bool {
return (self as Date).isSameDay(date: date as Date?)
}
/**
* Returns whether two dates fall on the same month.
*
* - parameter date: NSDate to compare with sender
*
* - returns: True if both paramter dates fall on the same month, false otherwise
*/
public func isSameMonth(date: NSDate?) -> Bool {
return (self as Date).isSameMonth(date: date as Date?)
}
}
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let start = dateFormatter.date(from: "2017-06-12T00:00:00.000Z")
let end = dateFormatter.date(from: "2017-06-12T22:59:59.999Z")
print(start?.isSameDay(date: end) ?? false)
print(Calendar.current.isDate(start!, inSameDayAs: end!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment