0

This code worked well; I had no issues with it until last week, when I flew to another state and opened my app to find that all of my databases were vanished. I'm not sure why this happened, but when I go home, my app works perfectly again. I figured out what was wrong, and it was a time zone change. Can you teach me how to get the date without using a time? I only care about the year, month, and day, but not the hour, minute, second, or time zone. Where can I get code that disregards the time?

My simplest code below

func getJournalData(forDate: Date) -> The_journal_data? {
    let theDateCom = Calendar.current.dateComponents([.year, .month, .day], from: forDate)
    let freshDate = Calendar.current.date(from: theDateCom)
    let getRawData = theJournalData.first(where: { $0.date == freshDate })
    
    return getRawData
}
HangarRash
  • 7,314
  • 5
  • 5
  • 32
Travgalax
  • 81
  • 1
  • 7
  • 1
    Does this answer your question? [Comparing NSDates without time component](https://stackoverflow.com/questions/24577087/comparing-nsdates-without-time-component) – HangarRash Apr 05 '23 at 19:51
  • It does not solve my question. I'm not sure how to use that code with a filter. Because it tells me whether or not the dates are match. That is not what I am referring to. I discuss how to find my database using a date but ignore the time. I need to search a database for a date within an array. – Travgalax Apr 05 '23 at 19:56
  • You just replace `$0.date == freshDate` with `Calendar.current.isDate($0.date, equalTo: freshDate, toGranularity: .day)`. Your question is about comparing dates without time. The linked question tells you how to do that. – HangarRash Apr 05 '23 at 19:59
  • Now I understand. I tested it, and it now works, with the exception that it cannot discover today's database but can get databases from yesterday and the past. – Travgalax Apr 05 '23 at 20:09
  • I just discovered that when I shift time zones to the east, such as from mountain time to east time, it works perfectly, but when I go from mountain time to California time, it is unable to locate my today database. Do you have any ideas? – Travgalax Apr 05 '23 at 20:34
  • You need to provide a lot more details about your date usage. Specifically `theJournalData.date` and the value of `forData` being passed into `getJournalData`. But first, try getting rid of the `theDataCom` and `freshDate` lines and pass `forDate` directly into the use of `Calendar.current.isDate($0.date, equalTo: forDate, toGranularity: .day)`. – HangarRash Apr 05 '23 at 20:45
  • Note, from the docs: https://developer.apple.com/documentation/foundation/date Date is a specific point in time, **independent of** any calendar or **time zone**. It is as if, all dates are in UTC, timezone zero. Show a minimal reproducible code that produces your issue, see: [minimal code](https://stackoverflow.com/help/minimal-reproducible-example) – workingdog support Ukraine Apr 06 '23 at 01:41

0 Answers0