Home > Software design >  Number of days in month returns wrong value after 10:00 PM
Number of days in month returns wrong value after 10:00 PM

Time:12-07

I am having a small issue with getting the total days in a month using Swift. I have extended the Date class and created this function:

    func daysInMonth() -> Int {
        print(self.day)  ##30
        print(self.month) ##12
        print(self) ## 2021-11-30 23:46:29  0000
        print(Calendar.current.range(of: .day, in: .month, for: self)?.count) ##31
        return Calendar.current.range(of: .day, in: .month, for: self)?.count ?? 0
    }

I have set the Date&Time to the 30th of November, at 11:45 PM in the settings of my Mac, in Preferences. I called the above function at 11:46 PM and obtained the above results (inline, next to the print statements). The date output is correct as well as the day. The month output is wrong and the result is 31 days in the month of November. If I run this exact same code before 10:00 PM, I get the right result which is 30 days.

Does anyone know why this is happening?

Thank you,

Paprika

CodePudding user response:

It's a GMT offset issue combined with the current day in a month.

When you create a date without set a day, it will be set to the first day of the month.

So, if your timezone offset is for example -4 means your are 4 hours behind the GMT 0 and by default the timezone defined at Calendar.current is equal the system timezone. So what it means? Means you'll obtain the previous month if you test it in a boundary of 23 (-4) or the next month if your offset is positive.

You can test this behaviour copying'n paste the following code in the Playground.

func getDaysInMonth(month: Int, year: Int, offset: Int = 0) -> Int? {
    let someDate = DateComponents(year: year, month: month, hour: 3)
    var current = Calendar.current
    let timezone = TimeZone(secondsFromGMT: 60 * 60 * offset)!
    current.timeZone = timezone
    guard let someDay = current.date(from: someDate) else { return nil }
    print("date: \(someDay)") // this will always
    return someDay.daysInCurrentMonth
}

for hour in -12...12 {
    print("hour: \(hour)\ndays: \(getDaysInMonth(month: 10, year: 2021, offset: hour) ?? -1)")
    print("---\n")
}

extension Date {
    var daysInCurrentMonth: Int? {
        Calendar.current.range(of: .day, in: .month, for: self)?.count
    }
}

Notice the days will change starting by your current system time zone (notice only the month will change).

How to fix this?

In your case, I guess you just want to show how many days a month have, so you can just set the to zero like this:

TimeZone(secondsFromGMT: 0)

Do this change at a instance of Calendar.current and check if it works for you.

CodePudding user response:

It appears there something wrong with your Date extension methods for .day and .month. Without seeing code it's hard to determine what the problem is though. Below is some code for returning the current month (Int) and current numbered day of month (Int)

extension Date
{
    var month: Int
    {
        let date = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.month], from: date)
        return components.month
    }
    
    var day: Int
    {
        let date = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.day], from: self)
        return components.day
    }
}

Please also ensure your time/date settings are correct on your mac/simulator/device. If these are wrong - it could have been jumping to a different month if you were in a timezone that was ahead a few hours.

  • Related