Created
January 18, 2015 16:17
-
-
Save ilvcs/8452b650fcab21876073 to your computer and use it in GitHub Desktop.
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 UIKit | |
import Foundation | |
class ViewController: UIViewController { | |
var myArray = [String]() | |
@IBOutlet weak var mydatePicker: UIDatePicker! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func mydatepickerAction(sender: AnyObject) { | |
var daFormet = NSDateFormatter() | |
//For ex: 2015:01:17:20:05 | |
daFormet.dateFormat = "YYY:MM:dd:HH:mm" | |
//This makes datestring from Datepickersdate | |
var dateString:String = daFormet.stringFromDate(mydatePicker.date) | |
//Makes an array from the date String | |
myArray = dateString.componentsSeparatedByString(":") | |
} | |
@IBAction func myButton(sender: AnyObject) { | |
println(myArray) | |
//Creates a date from components | |
var newMyDate:NSDate = dateFromComponents(myArray[2] .toInt()!, month: myArray[1] .toInt()!, year: myArray[0] .toInt()!, hour: myArray[3] .toInt()!, minute: myArray[0] .toInt()!, second: 00) | |
println(newMyDate) | |
var date = NSDate() | |
println(date) | |
} | |
func dateFromComponents(day:Int, month:Int, year:Int, hour:Int, minute:Int, second:Int) -> NSDate { | |
var cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)! | |
cal.timeZone = NSTimeZone.localTimeZone() | |
var comps = NSDateComponents() | |
comps.day = day | |
comps.year = year | |
comps.second = second | |
comps.hour = hour | |
comps.minute = minute | |
comps.month = month | |
return cal.dateFromComponents(comps)! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment