1

I've seen this post for other languages but not for swift. I have a date saved in the format of 2015-08-31 21:36:00 +0000 and I'm able to extract the day, month, year and weekday with the code below to produce Monday, August 31, 2015. When I try to use:

let hourInt = components.hour
var hourString = String(hourInt)

It prints a four hour difference. In this case "17" for the "21". How do I display it as 9:36 P.M.?

let flags: NSCalendarUnit =  NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour |  NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekday | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear
let date = NSDate()
let dateFormatter: NSDateFormatter = NSDateFormatter()
let components = NSCalendar.currentCalendar().components(flags, fromDate: array.date)


let weekday = components.weekday
let weekdays = dateFormatter.weekdaySymbols
let weekdayString = weekdays[weekday-1] as! String

let month = components.month
let months = dateFormatter.monthSymbols
let monthString = months[month-1] as! String

let dayInt = components.day
var dayString = String(dayInt)

let year = components.year
let yearString = String(year)
println(weekdayString + ", " + monthString + " " + dayString + ", " + yearString)
Suraj Sonawane
  • 2,044
  • 1
  • 14
  • 24
Pablo Picasso
  • 251
  • 1
  • 4
  • 13
  • Use NSDateFormatter, and remember to set the timezone. NSDates are absolute points in time, and don't know about timezones. See http://stackoverflow.com/questions/24255020/swift-nsdate-formatting-with-strftime-localtime/24256109#24256109 – Grimxn Jun 28 '15 at 17:33

4 Answers4

3

Your date string suffix +0000 means it is UTC time if you want to display time at UTC you need to specify it when setting your date formatter.

let dateString = "2015-08-31 21:36:00 +0000"
let df = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
if let date = df.dateFromString(dateString) {
    // now you have your date object
    // to display UTC time you have to specify timeZOne UTC
    df.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    df.dateFormat = "EEEE, MMMM dd, yyyy h:mm:ss a"
    let stringFromDate = df.stringFromDate(date)
    println(stringFromDate)   // "Monday, August 31, 2015 9:36:00 PM"
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

If you want to strip the time zone information, pass the GMT time zone. This code does quite the same as yours

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
println(dateFormatter.stringFromDate(array.date))
vadian
  • 274,689
  • 30
  • 353
  • 361
1

Setting locale of NSDateFormatter to en_US_POSIX fixed issue for me.

let sharedFormatter:NSDateFormatter = {
    let formatter = NSDateFormatter()
    formatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
    return formatter
}()

sharedFormatter.dateFormat = "MMM d YYYY, h:mm a"
let dateString = sharedFormatter.stringFromDate(NSDate()) 
// Aug 2 2016, 5:45 PM
Digitech
  • 262
  • 2
  • 9
0

Swift 5.0

let dateString = "2015-08-31 21:36:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

if let date = dateFormatter.date(from: dateString) {
    // now you have your date object
    // to display UTC time you have to specify timeZOne UTC
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy h:mm:ss a"
    let stringFromDate = dateFormatter.string(from: date)
    print(stringFromDate)   // "Monday, August 31, 2015 9:36:00 PM"
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40