0

I have the following code:

 for dates in calendar.selectedDates {
        for dateOfEvent in myEventDate {
            formatter.dateFormat = "dd MM yyyy"
            let dateToCompare = formatter.string(from: dates)

            if dateOfEvent == dateToCompare {
                formatter.dateFormat = "dd MM yyyy"
                let comparingDate = formatter.date(from: dateToCompare)
}

Does anyone could please explain to me why comparingDate is nil?? I'm loosing it...

UPDATE-------- In this for loop everything is working fine. I'm getting the date selected by the user from a calendar (dates) and dateOfEvent is a date (string) which get from coredata. Until the if statement everything is working fine. When I try to format the dates in this format "dd MM yyyy" the the result is nil

enter image description here

--------UPDATE

or dates in calendar.selectedDates {
        for dateOfEvent in myEventDate {
            formatter.dateFormat = "dd MM yyyy"
            let dateToCompare = formatter.string(from: dates)

            if dateOfEvent == dateToCompare {

                formatter.timeZone = Calendar.current.timeZone
                formatter.locale = Calendar.current.locale
                formatter.dateFormat = "dd MM yyyy"
                let comparingDate = formatter.date(from: dateToCompare)
                print(comparingDate)
Eric Aya
  • 69,000
  • 34
  • 174
  • 243
Marco
  • 991
  • 15
  • 37
  • 1
    Show the date string – mag_zbc Sep 14 '17 at 13:31
  • 2
    Never use date strings for data comparison. Compare `Date` objects when comparing objects. – Dávid Pásztor Sep 14 '17 at 13:32
  • @mag_zbc the date string? DateToCompare is formatted like so e.g. "12 09 2017" i'm just trying to get a date from dates which has this format: "dd MM yyyy" – Marco Sep 14 '17 at 13:34
  • It's working for me. Did you try setting the formatter's locale to `EN_US_POSIX`? – Tamás Sengel Sep 14 '17 at 13:35
  • @DávidPásztor this is what i'm trying to do... but in order to do so i need a date formatted in "dd MM yyyy" way – Marco Sep 14 '17 at 13:35
  • @the4kman i'm using the formatter all over the viewcontroller and it's working for every other date... but this... – Marco Sep 14 '17 at 13:36
  • 1
    `calendar.isDate(date1, inSameDayAs: date2)` might be useful. – Martin R Sep 14 '17 at 13:36
  • @MartinR thanks for the answer... but i cannot use this... the question is connected to the other question you are helping me with... :) https://stackoverflow.com/questions/46217293/swift-coredata-format-date-and-use-it-in-predicate today i'm loosing it... – Marco Sep 14 '17 at 13:37
  • @Marco that's not what your code does. Here: `if dateOfEvent == dateToCompare` you are comparing date strings and not `Date` objects. Moreover, without providing us some sample input for which your code is not working, it's really hard to help you. – Dávid Pásztor Sep 14 '17 at 13:38
  • @DávidPásztor you are right... sorry... i'll edit the question with some code... – Marco Sep 14 '17 at 13:39
  • 1
    ... In other words, a [mcve]. – Martin R Sep 14 '17 at 13:39
  • I know guys you are right... but to be honest i wouldn't know how to explain it differently... dateToCompare is a string and is there (if i print it it's fine). When i try to convert the string (dateToCompare) in date then i get nil – Marco Sep 14 '17 at 13:43
  • i managed to have not nil anymore... but the formatter is not working... attaching pic to the question – Marco Sep 14 '17 at 13:51
  • @Marco: Re your comment *"so if i want to compare it without time i need to use string?"* in the now deleted answer: No, nobody said that, quite the contrary. You might re-read my above comment about `isDate(.., inSameDayAs)` and David's comment *"Never use date strings for data comparison"* – Martin R Sep 14 '17 at 14:27
  • @MartinR trust me i really appreciate all the effort that you are putting in it... and i know u will start to hate me.... but ho can i use that if: i am comparing date with the predicate? fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.agendaDates == %@", date as CVarArg) this date as CVarARg is the date i need to pass (which is selected form the calendar).. probably is getting too confusing and i should write another question... trying to explain everything in a better way... – Marco Sep 14 '17 at 14:31
  • You are mixing the problems again. This question is about date conversions (and I admit it is still unclear to me what you want to achieve or what you expect). – Your other question https://stackoverflow.com/questions/46217293/swift-coredata-format-date-and-use-it-in-predicate is about Core Data fetch requests to get events in a certain time interval. – Martin R Sep 14 '17 at 14:33
  • @MartinR i know i am sorry... this getting confusing. So i'm writing a question to wrap it up properly and make some clear. I'll post the link here... sorry for the confusion and thanks! – Marco Sep 14 '17 at 14:34
  • @MartinR here it is: https://stackoverflow.com/questions/46222493/core-data-comparing-date-from-coredata-object-to-a-given-date hopefully this will clarify... thx! – Marco Sep 14 '17 at 15:03

0 Answers0