【问题标题】:How to change the current day's hours and minutes in Swift?如何在 Swift 中更改当天的小时和分钟?
【发布时间】:2016-03-17 23:49:32
【问题描述】:

如果我创建一个Date() 来获取当前日期和时间,我想从中创建一个新日期,但小时、分钟和零秒不同,那么使用 Swift 最简单的方法是什么?我找到了很多关于“获取”而不是“设置”的例子。

【问题讨论】:

  • 你试过NSDateFormatter吗?并从中使用dateFromString
  • 我认为这个答案比转换为字符串并再次返回要好。这就是为什么我在我的问题中问“最简单的方法”。我知道有几种方法可以做到这一点,但我认为最好使用组件。

标签: swift nsdate


【解决方案1】:

请注意,对于使用夏令时的语言环境,在时钟更改日,某些时间可能不存在,或者可能出现两次。以下两种解决方案都返回 Date? 并使用强制展开。你应该在你的应用中处理可能的nil

Swift 3、4、5 和 iOS 8 / OS X 10.9 或更高版本

let date = Calendar.current.date(bySettingHour: 9, minute: 30, second: 0, of: Date())!

斯威夫特 2

使用NSDateComponents/DateComponents

let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let now = NSDate()
let components = gregorian.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: now)

// Change the time to 9:30:00 in your locale
components.hour = 9
components.minute = 30
components.second = 0

let date = gregorian.dateFromComponents(components)!

请注意,如果您调用print(date),打印的时间是UTC。这是同一时刻,只是在与您的时区不同的时区中表达。使用NSDateFormatter 将其转换为您的当地时间。

【讨论】:

  • Swift 4 怎么样?
  • @AhmetAkkök Swift 3 示例也适用于 Swift 4
  • 如何改变他的上午和下午
  • @A.s.ALI 你只需增加 12 小时:let date = Calendar.current.date(byAdding: DateComponents(hour:12), to: Date())
【解决方案2】:

带有时区的 swift 3 日期扩展

extension Date {
    public func setTime(hour: Int, min: Int, sec: 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 = hour
        components.minute = min
        components.second = sec

        return cal.date(from: components)
    }
}

【讨论】:

  • 我认为你必须在组件中包含.timeZone
【解决方案3】:
//Increase the day & hours in Swift

let dateformat = DateFormatter()
let timeformat = DateFormatter()
        
dateformat.dateStyle = .medium
timeformat.timeStyle = .medium

//Increase Day

let currentdate = Date()

let currentdateshow = dateformat.string(from: currentdate)
textfield2.text = currentdateshow

let myCurrentdate = dateformat.date(from: dateTimeString)!
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: myCurrentdate) // Increase 1 Day

let tomorrowday = dateformat.string(from: tomorrow!)
text3.text = tomorrowday
text3.isEnabled = false
  
//increase Time  
    
let time = Date()

let currenttime = timeformat.string(from: time)
text4.text = currenttime
        
let mycurrenttime = timeformat.date(from: currenttime)!
let increasetime = Calendar.current.date(byAdding: .hour, value: 2, to: mycurrenttime) //increase 2 hrs.

let increasemytime = timeformat.string(from: increasetime!)
text5.text = increasemytime

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多