0
votes

How can I make the date picker show the date and time that is now? Without clicking nothing, just at the beginning. I am very new in Cocoa, I would appreciate any orientation.

This is what I have done:
- I create a new project > OSX > Application > Cocoa Application
- Main.storyboard. Drag a Date Picker
- Ctrl + drag from date picker to ViewController.swift. I give it the name datePicker: @IBOutlet weak var datePicker: NSDatePicker!

2
Add [datePicker setDateValue:[NSDate new]]; in viewdidLoad of View Controller - Kaunteya
@Kaunteya this gives me an error. Are you sure this is Swift? - Nrc
Oh sorry. You can datePicker.dateValue = NSDate() would solve your problem. Please get your basics right. It is not very hard to map obj-c to swift - Kaunteya
It works, thank you. You should put that in the answer. I will accept it - Nrc

2 Answers

1
votes

Add [datePicker setDateValue:[NSDate new]]; in viewdidLoad of View Controller

0
votes

Here is a function that returns the hour and the day and corrects for different time zones, I am using 7 for Nevada PST time zone but you can update for your specific state

 func returnHourDay() -> (Int, Int) {
    let dateNow = Date()
    let calendar = Calendar.current
    var Hour = (calendar as NSCalendar).component(NSCalendar.Unit.hour, from: dateNow)
    let Day = (calendar as NSCalendar).component(NSCalendar.Unit.weekday, from: dateNow)

    //correct for specific time zones
    func ltzOffset() -> Int { return NSTimeZone.local.secondsFromGMT() }
    let hourOffset = 7 + ltzOffset()/60/60  //0 for Nevada 7 - 7 = 0
    Hour = Hour+hourOffset

    return (Hour, Day)
}