On the screen I have UIDatePicker. It would be simple if there was simple picker. It could be like that:
app.pickeWheels["my_id_picker"].adjustToPickerWheelValue("expected_value")
but the question is how to adjust multiply pickers within UIDatePicker?
You will have to access the different wheels by specifying what element/component of the picker you want to adjust.
Here is my code (in Swift 2.2) with a much simpler date picker but I believe you can adjust the code to meet your needs:
let calendar = NSCalendar.currentCalendar()
let date = NSDate()
let month = calendar.component(NSCalendarUnit.Month, fromDate: date)
let day = calendar.component(NSCalendarUnit.Day, fromDate: date)
let nextMonthName = dateFormatter.monthSymbols[month]
let firstWheel = app.pickerWheels.elementBoundByIndex(0)
firstWheel.adjustToPickerWheelValue("\(nextMonthName)")
let secondWheel = app.pickerWheels.elementBoundByIndex(1)
secondWheel.adjustToPickerWheelValue("\(day+1)")
Related
I am using Swift 3 and trying to get the current hour. Below is the code i have written to get the current hour but it gives an error.
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date) // error
What could be the reason for this, all the sample code based on Swift 3 shows the same code snippet to get the current hour but it just don't seem to work. Please can anyone point out what could be wrong here. Thanks.
Try this code to get hour in swift 3 :
let calendar = NSCalendar.current
let hour = calendar.component(.hour, from: NSDate() as Date)
Use this code to get a current hour:
let date = Date()
let formatter = NSDateFormatter()
formatter.setFormat = "h"
print(formatter.stringFromDate(date))
Note: I did not run this code in xcode so don't just copy paste.
Consider the following code:
import UIKit
let date = Date()
guard let nycTimeZone = TimeZone(abbreviation: "EST"),
let nzTimeZone = TimeZone(abbreviation: "NZDT") else {
fatalError()
}
var nycCalendar = Calendar(identifier: .gregorian)
nycCalendar.timeZone = nycTimeZone
var nzCalendar = Calendar(identifier: .gregorian)
nzCalendar.timeZone = nzTimeZone
let now = Date()
let nycDayOfEra = nycCalendar.ordinality(of: .day, in: .era, for: now)
let nycDayOfYear = nycCalendar.ordinality(of: .day, in: .year, for: now)
var nzDayOfEra = nzCalendar.ordinality(of: .day, in: .era, for: now)
var nzDayOfYear = nzCalendar.ordinality(of: .day, in: .year, for: now)
As I write this, NYC time and Aukland NZ time give different days. That's the case I'm interested in.
With the code above, the results for nycDayOfYear and nzDayOfYear are different (as of this writing I get nycDayOfYear=42 and nzDayOfYear=43.)
That is as expected, and as desired. (I was working to answer a "how do I calculate the number of days of difference in two Dates evaluated in different time zones?" question.)
However, it would take a bunch of messy adjustments to make the above day-of-year calculation and figure out the number of days of difference between those local dates when they span year boundaries.
I therefore tried to do the calculations using ordinality(of: .day, in: .era, for: date).
However, the calculations based on calendar era give the same value regardless of the time zone of the calendar used to make do the calculation.
Why is that?
What would be a simpler way to calculate the number of calendar days difference between two dates WHEN EXPRESSED IN DIFFERENT LOCAL TIME ZONES? Like I said, my code that calculates the day of year would need additional logic added to handle dates that span calendar year boundaries.
Note that this is a different question than "How many days difference is there between 2 dates". In my question I want both dates to be expressed in different local time zones, and I'm interested in the difference in the calendar date of each of those date values.
Martin's comment about calendar calculations over long intervals giving unexpected results is as good an answer as any as to why it doesn't work.
I did come up with code that calculates the desired difference in calendar date values between 2 dates expressed in specific time zones:
let date = Date()
guard let nycTimeZone = TimeZone(abbreviation: "EST"),
let nzTimeZone = TimeZone(abbreviation: "NZDT") else {
fatalError()
}
var nycCalendar = Calendar(identifier: .gregorian)
nycCalendar.timeZone = nycTimeZone
var nzCalendar = Calendar(identifier: .gregorian)
nzCalendar.timeZone = nzTimeZone
let now = Date()
let nycDateComponents = nycCalendar.dateComponents([.month, .day, .year], from: now)
let nzDateComponents = nzCalendar.dateComponents([.month, .day, .year], from: now)
let difference = Calendar.current.dateComponents([.day],
from: nycDateComponents,
to: nzDateComponents)
let daysDifference = difference.days
First I convert the 2 dates to month/day/year DateComponents values using calendars set to their specific time zone.
Then I use the Calendar function dateComponents(_:from:to:), which lets you calculate the difference between 2 DateComponents values, in whatever units you want to use to compare them. (days, in this case)
I'm working on a project where I have labels and images that change on a daily basis. My idea on how to go about this is I add assets for the images and have multiple files where I take the text (that contains the quote).
Two questions:
Is there a better way of approaching this? (I'm fairly new to iOS, so I'm wondering if this is sound).
How can I take the current day as in: May 22? (I just want to know how to get "22").
you need to use NSDate
// Option 1
let date = NSDate() // today
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateFormat = "dd"
let d = dateStringFormatter.stringFromDate(date)
// d = "22"
// Option 2
let d = NSCalendar.currentCalendar().component(.Day, fromDate: NSDate()) // thanks Leo-Dabus
Reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/
Try taking a look at this stackoverflow link on getting the current date.
How to get the current time as datetime
According to noliv in the article you should be able do :
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute
Hope this helps
I'm having difficulties to get the right now command with the Swift 2.0, my command lists are below:
let dateNow = NSDate()
let calendar = NSCalendar.currentCalendar()
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow)
let minute = calendar.component(NSCalendarUnit.Minute, fromDate: dateNow)
The error said that instances calendar cannot be used in ViewController, and what confuses me is that the line above (the let calendar = ...) clearly defines calendar as a constant but why cannot I access it for my let hour and minute? please help
Just barely getting into iOS development using swift and getting a little overwhelmed sometimes trying to look at documentation and understand it. I am also currently running through Simon Allardice's iOS app development with swift essential training on Lynda.com and had a question regarding one of the objects we have instantiated in the WhatDay example.
We basically are setting up a UIDatePicker object from which we are extracting what day of the week we are looking at with the NSDateFormatter object at which point I was wondering,
which property would we need to access to get ahold of the current year that the user scrolled the wheel to?
So far we have this code to access the day of the week it was,
#IBACTION func displayDay(sender: AnyObject) {
// grab the selected data from the date picker
var chosenDate = self.datePicker.date
// create an NSDateFormatter
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE"
// grab the day and create a message
let day = formatter.stringFromDate(chosenDate)
let result = "That was a \(day)"
}
Also, he says we can use the date formatter "EEEE" to format for day of the week but I couldn't find any documentation on that online what the string codes are, any advice on where to find this information?
You can use NSCalendar for components of a date.
If you want to format you date as you did, then please look into this doc for different date formatting string
// grab the selected data from the date picker
var chosenDate = self.datePicker.date
//use NSCalenda
let myCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let myComponents = myCalendar?.components(.WeekdayCalendarUnit | .YearCalendarUnit, fromDate: chosenDate)
let weekDay = myComponents?.weekday
let year = myComponents?.year
println("year:\(year) , weekday:\(weekDay)")
var datePicker = UIDatePicker()
use it