-6

I want to have the current date prepopulate in the navigation bar each time the user opens the app, without having to select it using the date picker. Similar to how food tracker apps such as Lose It and MyFitness Pal work. But I can't find any documentation on how to do this. Here's what I tried but it says I can't assign NSDate to type String.

let currentDate = NSDate()
let dateFormatter = NSDateFormatter()

 dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
    var convertedDate = dateFormatter.stringFromDate(currentDate)
    dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
    convertedDate = dateFormatter.stringFromDate(currentDate)

    self.title = NSDate()
Wyetro
  • 8,439
  • 9
  • 46
  • 64
M.J.
  • 23
  • 1
  • 7
  • `NSDate()` will give a date object set to the current date and time. `NSDateFormatter` is used to get string representations of dates – Paulw11 Sep 07 '16 at 20:32
  • You'll want something like `self.title = NSDate()` – Wyetro Sep 07 '16 at 20:33
  • 1
    Possible duplicate of [How to get the current time (and hour) as datetime](http://stackoverflow.com/questions/24070450/how-to-get-the-current-time-and-hour-as-datetime) – Avt Sep 07 '16 at 22:44
  • Hi @WMios I converted current date to a string as described above but it's not letting me assign it to the title saying it's still not a string. I added my code above thx :) – M.J. Sep 09 '16 at 13:14
  • It needs to be "/(NSDate())" or String(NSDate()) – Wyetro Sep 10 '16 at 00:13
  • @WMios ok String(NSDate()) worked I didn't realize I was actually putting it in a print format BUT one more issue. I'm getting 2016-09-12 15:40:06+0000 even though I've specified the format I want above. I took out the redundancy .FullStyle but there's still no change. – M.J. Sep 12 '16 at 16:46
  • Yeah so you want to make self.title equal to your formatted date. – Wyetro Sep 13 '16 at 02:17
  • @WMios I'm sorry but I don't understand – M.J. Sep 13 '16 at 18:20
  • Just do `self.title = convertedDate` – Wyetro Sep 13 '16 at 18:35
  • @WMios wow thank you so much!!! I feel like such a dummy lol. – M.J. Sep 13 '16 at 18:49
  • @WMios is there a way to contact you? I'm looking for someone I can pay for simple help just like this. – M.J. Sep 13 '16 at 18:52
  • @M.J., if you want, click on my username and go to my profile and I have a link to my site that has a contact page. I don't want to post info here. – Wyetro Sep 13 '16 at 19:07

1 Answers1

0

The issue is that you need to assign self.title the correct date value.

The line:

self.title = NSDate()

is assigning self.title to a new instance of NSDate(), while you want to assign it to the formatted date that you have above, convertedDate, which is also aString`.

Additionally, NSDate() is of type NSDate and not of type String. If you converted it to a String it would be unformatted.

So really, you need the line to be:

self.title = convertedDate
Wyetro
  • 8,439
  • 9
  • 46
  • 64