melvitax / DateHelper

A Swift Date extension helper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dateWith function is causing creating incorrect dates

hobbesthetige opened this issue · comments

Today is 28 Feb 2017. If I write a simple line

   `let startDate = Date().dateFor(.startOfDay)
    
    let endDate = Date().dateFor(.endOfDay)
    
    print(startDate.toString(style: .full) + " to " + endDate.toString(style: .full))`

The result I get is

Friday, February 3, 2017 at 12:00:00 AM Eastern Standard Time to Friday, February 3, 2017 at 11:59:29 PM Eastern Standard Time

This shouldn't be the 3rd of Feb but the 28th.

It looks like in your function dateWith... you're assigning a weekday component to a day component. If you match day = day, the correct date appears. I don't know what else this will break things elsewhere tho.

func dateWith(hour: Int?, minute: Int?, second: Int?, day: Int? = nil, month: Int? = nil) -> Date { var comp = Date.components(self) comp.month = month ?? comp.month comp.day = day ?? comp.weekday comp.hour = hour ?? comp.hour comp.minute = minute ?? comp.minute comp.second = second ?? comp.second return Calendar.current.date(from: comp)! }

Thanks. Just fixed it in c5e5dc0