0

I've racked my brain over this issue all day and can't seem to get a solution. I have a fitness program that queries the Health Data Store. In the predicateForSamples(withStart: myStartDate, end: myEndDate) I am finding that getting a date from a datePicker sends a moment in time and therefore my query does not return the results for the entire day as I'd like. So, I figured if I take that datePicker date and convert it to a starting and ending format my issue would be solved. However, using the let date = dateFormatter.date(from: dateString) is returning the correct date but the time returns as 04:00:00 +0000 for both functions.

If anybody has a suggestion, please feel free to lend a helping hand. Thank you so very much!

func convertStartDate(StartDate: Date) -> Date {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyy-MM-dd '00':'00':'01' +0000"
    let dateString = dateFormatter.string(from: StartDate)
    print("convertingStartDate() - \(dateString)")
    let date = dateFormatter.date(from: dateString)
    print(date as Any)

    return date!
}

func convertEndDate(EndDate: Date) -> Date {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyy-MM-dd '23':'59':'59' +0000"
    let dateString = dateFormatter.string(from: EndDate)
    print("convertingEndDate() - \(dateString)")
    let date = dateFormatter.date(from: dateString)

    return date!
}
Be Know Do
  • 73
  • 1
  • 11
  • Here is the output: 2020-04-09 04:00:00 +0000 – Be Know Do Apr 09 '20 at 18:52
  • 1
    Why there are numbers in your date format pattern ?? – dahiya_boy Apr 09 '20 at 19:11
  • I wanted a specific time to capture all the data in Health Kit. So, I changed it to just after and just before midnight. – Be Know Do Apr 09 '20 at 19:21
  • The date is correct if your time zone is -04:00. `print` shows dates always in UTC (+0000). The time format `'00':'00':'01'` is syntactic sugar. – vadian Apr 09 '20 at 19:59
  • You should keep your date unchanged and if you would like to know if a date is in same day as another you can use Calendar method isDate inSameDayAs https://developer.apple.com/documentation/foundation/calendar/2292885-isdate and https://stackoverflow.com/a/43664156/2303865 – Leo Dabus Apr 09 '20 at 20:18

2 Answers2

1

This should fix your issue

func convertStartDate(StartDate: Date) -> Date {

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    dateFormatter.dateFormat = "yyy-MM-dd '00':'00':'01' +0000"
    let dateString = dateFormatter.string(from: StartDate)
    dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss +0000"
    let date = dateFormatter.date(from: dateString)
    print(date as Any)

    return date!
}

func convertEndDate(EndDate: Date) -> Date {

    let dateFormatter = DateFormatter()
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    dateFormatter.dateFormat = "yyy-MM-dd '23':'59':'59' +0000"
    let dateString = dateFormatter.string(from: EndDate)
    dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss +0000"
    let date = dateFormatter.date(from: dateString)

    return date!
}
Mohamed Mostafa
  • 1,057
  • 7
  • 12
  • The output came back as 2020-04-09 04:00:01 +0000 for convertStartDate when expecting 2020-04-09 00:00:01 +0000. The same issue with the convertEndDate. Any ideas of what is happening? Thank you! – Be Know Do Apr 09 '20 at 19:37
  • it is a timezone issue try setting dateFormatter.locale = Locale(identifier: "en_US_POSIX") – Mohamed Mostafa Apr 09 '20 at 19:46
  • Sorry. It's still returning the wrong timezone. I truly appreciate the help you are providing. – Be Know Do Apr 09 '20 at 19:52
  • You should always set your locale to `en_US_POSIX` before setting the dateFormat when using fixed date formats otherwise your dateFormat will reflect the user device locale and settings – Leo Dabus Apr 09 '20 at 20:12
  • 1
    Yes, I mixed locale with timezone line of code, And adding locale and time zone is a good practice and i recommend also having a look at https://williamboles.me/sneaky-date-formatters-exposing-more-than-you-think/ – Mohamed Mostafa Apr 09 '20 at 20:14
  • Here's what I just discovered. Health Kit must save it's data in UTC time because once I adjusted the time as recommended, the results of my query was wrong. When I changed it back to UTC, it gave the correct information. I appreciate all the assistance with this. I got the result I was looking for. Thank you! – Be Know Do Apr 09 '20 at 20:25
1

In addition to above, this is actually fixed by setting dateFormatter.timeZone = TimeZone(secondsFromGMT: 0), as the seconds from GMT is your issue.

pkamb
  • 33,281
  • 23
  • 160
  • 191