How to get the specific date in iOS? - ios

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

Related

cannot convert value of type date to expected argument type date

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.

Date format Swift 3.0

I have the following code
var calendar = Calendar.current
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
calendar.timeZone = TimeZone(identifier: "UTC")!
let startHour = calendar.component(.hour, from: cell.startDate as Date)
let startMinutes = calendar.component(.minute, from: cell.endDate as Date)
let endHour = calendar.component(.hour, from: cell.endDate as Date)
let endMinute = calendar.component(.minute, from: cell.endDate as Date)
print("start hours = \(startHour) : \(startMinutes)")
print("end hours = \(endHour) : \(endMinute)")
So, the result has been right but in the wrong format. It prints 22:30, for instance, which is what I want, but when it comes to numbers less than 10, like 2 AM for instance, it prints 2:0 instead of 02:00. How can I solve this problem? I am trying to link dateformatter to calendar but I can't. I also tried to solve this by taking the approach below:
let myFormatter = DateFormatter()
myFormatter.dateFormat = "HH:mm"
print(myFormatter.string(from: cell.startDate))
print(myFormatter.string(from: cell.endDate))
But it is printing hours that have nothing to do with the exact hours. Have no idea why.
So, any help would be much appreciated!
There are various ways to add the missing leading zero, however the best solution is using the DateFormatter. You have only one problem with the formatter - setting the correct time zone.
// safer than using identifiers that are not actually standardised
myFormatter.timeZone = TimeZone(secondsFromGMT: 0)

UITests Xcode7: How to adjust date picker with multiply pickers?

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)")

Swift Accessing year from UIDatePicker

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

Output for components.minutes unexpected. iOS Swift

Hi I am trying to get the current hour and minute of the day using the following code:
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour, fromDate: NSDate())
// Concatenate time and minutes together
let hour = components.hour
let minute = components.minute
println(components.hour)
println(components.minute)
The output for both:
components.hour output: 0
components.minute output: 9223372036854775807
It is currently 00.30 where I am. But why does components.minute print such a long number that doesn't to me seem to be the minute 30.
Also if anyone can suggest another way of getting the current time as e.g. 13:00?
Any help is much appreciated thanks!
9223372036854775807 is NSUndefinedDateComponent value. This means that NSDateComponents object doesn't have the value.
You have to join desired NSCalendarUnits with |:
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: NSDate())
OR, from iOS 8, we have handy component(_:fromDate:) method to get just one component's value.
let now = NSDate()
let hour = calendar.component(.CalendarUnitHour, fromDate: now)
let minute = calendar.component(.CalendarUnitMinute, fromDate: now)

Resources