Created
April 23, 2020 15:48
-
-
Save StevenMasini/af8b8d13241aa7eb33edb9f482b53fc9 to your computer and use it in GitHub Desktop.
Create random Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func generateRandomDate(daysBack: Int)-> Date?{ | |
let day = arc4random_uniform(UInt32(daysBack))+1 | |
let hour = arc4random_uniform(23) | |
let minute = arc4random_uniform(59) | |
let today = Date(timeIntervalSinceNow: 0) | |
let gregorian = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian) | |
var offsetComponents = DateComponents() | |
offsetComponents.day = -1 * Int(day - 1) | |
offsetComponents.hour = -1 * Int(hour) | |
offsetComponents.minute = -1 * Int(minute) | |
let randomDate = gregorian?.date(byAdding: offsetComponents, to: today, options: .init(rawValue: 0) ) | |
return randomDate | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment