0

i'm having an array fill with date string, which i would like to go through and check whether the date is today or yesterday. The date string could look like following:

2015-04-10 22:07:00

So far i've tried just to convert it using dateFormatter, but it keeps returning nil

    var dateString = arrayNews[0][0].date as NSString
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"

    var date = dateFormatter.dateFromString(dateString as String)
    println(date)

the sudo code i would like to look something like this

if dateString == currentDate
    date = "Today, hh:mm"
else if dateString == currentDate(-1)
    date = "Yesterday, hh:mm
else
    date = dd. MM, hh:mm

in the else statement the date could be 1. April, 12:00

How can i achieve such a logic?

Without the logic

func getDate(dateStr:String, format:String = "yyyy-MM-dd HH:mm:ss") -> NSString {
    var dateFmt = NSDateFormatter()
    dateFmt.timeZone = NSTimeZone.defaultTimeZone()
    dateFmt.dateFormat = format
    let newsDate = dateFmt.dateFromString(dateStr)!

    let date = NSDate();
    let dateFormatter = NSDateFormatter()


    dateFormatter.timeZone = NSTimeZone()
    let localDate = dateFormatter.stringFromDate(date)


    return ""
}
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • As others have pointed out, you want `HH`. As an aside, is this time, 22:07, i.e. 10:07pm, GMT or local time? Date formatters always assume it's local time, so I just wanted to make sure. But when saving times in persistent storage or exchanging with network service, it's prudent to include time zone information (or always store times in GMT/UTC/Zulu). – Rob Apr 15 '15 at 13:07

1 Answers1

4

You have to use yyyy-MM-dd HH:mm:ss instead of yyyy-MM-dd hh:mm:ss

  • HH: 24h format
  • hh: 12h format (with AM/PM)

Pay attention with dateFormatter, by default, it use localtime zone. println() shows the value for GMT time, which is hold by NSDate.

It means, without specifying timezone, when you convert your string 2015-04-10 22:07:00 to NSDatetime, it return a data which is the time at your local time zone is 2015-04-10 22:07:00. As NSDate holds date time in GMT, you will see a different value when you show the value of that NSDate with println().

If your timezone is GMT+2 (2h earlier than GMT), when it's 22h:07 in your place, the GMT time is 20h:07. When you call println() on that NSDate, you see 2015-04-10 20:07:00

To compare 2 NSDate:

let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)

let compareResult = calendar?.compareDate(date, toDate: date2, toUnitGranularity: NSCalendarUnit.CalendarUnitDay)

if (compareResult == NSComparisonResult.OrderedSame) {
    println("yes")
} else {
    println("no")
}

//to get yesterday, using NSCalendar:

let yesterday = calendar?.dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: -1, toDate: date, options: NSCalendarOptions.MatchStrictly)
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • okay this date is static and should not be changed regarding different timezone? will it do that? – Peter Pik Apr 15 '15 at 12:56
  • Yes, the value does not change. It's in GMT. But depending on how to show the value in your application (or console), you will get different display of that date. – Duyen-Hoa Apr 15 '15 at 13:00
  • I've now added the code above where localdate is the timezone datetime and newsDate is the date of the news. I now want to compare these to dates without the time, how is that possible to check whether its the same date or yesterday? – Peter Pik Apr 15 '15 at 13:56
  • to compare two NSDate, you should use NSCalendar (suppose that it's Gregorian calendar). See my updated answer – Duyen-Hoa Apr 15 '15 at 14:36