Nick3150
Nick3150

Reputation: 21

How to get the specific date in 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:

  1. Is there a better way of approaching this? (I'm fairly new to iOS, so I'm wondering if this is sound).

  2. How can I take the current day as in: May 22? (I just want to know how to get "22").

Upvotes: 0

Views: 175

Answers (2)

sonique
sonique

Reputation: 4772

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/

Upvotes: 2

Nate
Nate

Reputation: 188

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

Upvotes: 2

Related Questions