I have a string that I am pulling from the web in the format "Sat, Jan 30 @ 11:00 AM" and want to convert this to an NSDate. I am able to do so with the code below, but the year defaults to 2000.
How can I set the year to the current year for my NSDate?
var dateString = "Sat, Jan 30 @ 11:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, MMM dd @ hh:mm a"
let date = dateFormatter.dateFromString(dateString)
//date output is 2000-01-30 19:00:00 +0000
I believe that this is the Objective-C answer: "a date string without year to NSDate with year"
Thanks!
Not sure if that's the best way to do it, since I am kinda new to programming. But I would do something like that, works fine I guess (I know the order is kinda confusing, but I just did this in a few minutes without making the code "look good" ^^):
var dateString = "Sat, Jan 30 @ 11:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY EEE, MMM dd @ hh:mm a"
//let date = dateFormatter.dateFromString(dateString)
let currentDate = NSDate()
let newFormatter = NSDateFormatter()
newFormatter.dateFormat = "YYYY"
let YearString = newFormatter.stringFromDate(currentDate)
let newDateString = String(YearString + " " + dateString)
let date = dateFormatter.dateFromString(newDateString)
print(date)