ios - 夏令时月末日期问题

标签 ios swift date nsdatecomponents

月末日期给出加拿大渥太华时区的不同日期(夏令时)。

我正在尝试获取任何时区的月末日期。

注意:您可以通过更改时区设置(Mac 或在 iphone 中)加拿大渥太华来帮助我。并在 playground 中粘贴代码

extension Date {
    public func setTime(day: Int, month: Int,year:Int, timeZoneAbbrev: String = "UTC") -> Date {
        let x: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
        let cal = Calendar.current
        var components = cal.dateComponents(x, from: self)

        components.timeZone = TimeZone(abbreviation: timeZoneAbbrev)
        components.hour = 0
        components.minute = 0
        components.second = 0
        components.day = day
        components.month = month
        components.year = year

        return cal.date(from: components) ?? self
    }
    func getMonthGapDate(month: Int) -> Date {
        return Calendar.current.date(byAdding: .month, value: month, to: self)!
    }

    func startOfMonth() -> Date {
        return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
    }

    func endOfMonth() -> Date {
        return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
    }

}
let firstDayDate = Date().setTime(day: 1, month: 4, year: 2019)
let startDate = firstDayDate.getMonthGapDate(month: -1)
let endDate = firstDayDate.endOfMonth()
print(firstDayDate)
print(startDate)//Prints 2019-03-01 01:00:00 +0000(Ottawa - Canada time zone) Day light zone
print(endDate)// (This is issue)Prints 2019-03-31 04:00:00 +0000(Ottawa - Canada time zone) Day light zone//It should 2019 - 04 - 30

最佳答案

使用时区缩写可能会很麻烦,尽管“UTC”非常安全。

但是,我怀疑您应该使用 TimeZone.autoupdatingCurrent 来确保您获得本地午夜的日期。

extension Date {
    public func setTime(day: Int, month: Int,year:Int) -> Date {
        let x: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
        let cal = Calendar.current
        var components = cal.dateComponents(x, from: self)

        components.timeZone = TimeZone.autoupdatingCurrent
        components.hour = 0
        components.minute = 0
        components.second = 0
        components.day = day
        components.month = month
        components.year = year

        return cal.date(from: components) ?? self
    }
    func getMonthGapDate(month: Int) -> Date {
        return Calendar.current.date(byAdding: .month, value: month, to: self)!
    }

    func startOfMonth() -> Date {
        return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
    }

    func endOfMonth() -> Date {
        return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
    }

}

这给了我以下输出:

2019-04-01 04:00:00 +0000

2019-03-01 05:00:00 +0000

2019-04-30 04:00:00 +0000

请注意 +0000 - 日期以 UTC 显示,但代表本地午夜。

  • 三月初,渥太华使用夏令时,所以是 UTC-5
  • 三月底,渥太华使用夏令时,所以是UTC-4

关于ios - 夏令时月末日期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531801/

相关文章:

R(data.table)查找间隔中天数比例最大的日历月

objective-c - 查找NSMutableDictionary和NSMutableArray使用的实际内存空间

ios - 无法插入新的 socket 连接 : Could not find any information for the class named

javascript - iOS Safari 在 window.location.href = ... 上暂停动画?

ios - 如何在 init 中捕获错误(来自解码器 :Decoder) from a Codable struct?

java - 在 JSP 中将今天的日期显示为 "Today"而不是 "dd/mm/yyyy"

ios - Swift 中的照片框架升序

ios - 在调整 ScrollView 大小时滚动到 ScrollView 页面

ios - 使用 Swift 检索核心数据

linux - linux什么时候设置电脑的正确时间?