Swift - 获取当地日期和时间

时间:2015-02-09 06:43:45

标签: ios swift nsdate

如何在Swift中获取本地日期和时间?

let last_login = String(NSDate.date())

10 个答案:

答案 0 :(得分:43)

更新: Xcode 8.2.1•Swift 3.0.2

您还可以使用Date方法description(with locale: Locale?)来获取用户的本地化时间描述:

  

Date的字符串表示形式,使用给定的语言环境或语言环境   参数为零,国际格式为YYYY-MM-DD HH:MM:SS   ±HHMM,其中±HHMM表示以小时为单位的时区偏移量   UTC的分钟数(例如,“2001-03-24 10:45:32 +0600”)。

description(with locale: Locale?)

Date().description(with: .current)   // "Monday, February 9, 2015 at 05:47:51 Brasilia Summer Time"

上面的方法并不意味着在向用户显示日期和时间时使用。它仅用于调试目的。

向用户显示本地日期和时间(当前时区)时,您应该尊重用户区域设置和设备设置。您唯一可以控制的是日期和时间样式(短,中,长或完整)。有关详细信息,请查看此帖子shortDateTime

如果您打算为编码目的创建时间戳UTC(iso8601),可以查看此帖子iso8601

答案 1 :(得分:6)

通过设置格式

来使用NSDateFormatter
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm"
println(dateFormatter.stringFromDate(NSDate()))

或样式

dateFormatter.dateStyle = .NoStyle
dateFormatter.timeStyle = .MediumStyle

答案 2 :(得分:6)

我已经找到了答案。

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
let dateInFormat = dateFormatter.stringFromDate(NSDate())

答案 3 :(得分:4)

let expiryDate: Date = ...
let localizedDateString = DateFormatter.localizedString(from: expiryDate, dateStyle: .medium, timeStyle: .short)

“2017年9月10日,14:37”

答案 4 :(得分:2)

Leo的答案非常好。我只想添加一种方法将其用作计算属性。

 var currentTime: String {
    get {
        return Date().description(with: Locale.current)
    }
  }

像这样使用它:

print(currentTime)

答案 5 :(得分:2)

Swift 4

获取当前日期和时间

let currentDate = Date()
print(currentDate) //this will return current date and time 

但是将日期类型转换为字符串

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm" //give the formate according to your need
let dateStr =  dateFormatter.string(from: currentDate) //which will give the string of current date and time in required dateformate

答案 6 :(得分:2)

如果要获取 Date对象而不是字符串表示形式,则可以使用以下代码段:

extension Date {
    func localDate() -> Date {
        let nowUTC = Date()
        let timeZoneOffset = Double(TimeZone.current.secondsFromGMT(for: nowUTC))
        guard let localDate = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: nowUTC) else {return Date()}

        return localDate
    }
}

像这样使用它:

let now = Date().localDate()

答案 7 :(得分:1)

您必须使用NSDateFormatter

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy HH:mm"
dateFormatter.locale = "en" // Change "en" to change the default locale if you want  
let stringDate = dateFormatter.stringFromDate(date)

答案 8 :(得分:0)

要获取最常用的字符串格式(在处理查询和数据库时):

快捷键4

2019-01-09T01:07:04Z (GMT /祖鲁时间的RFC3339)

let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
let s = f.string(from: Date())

2019-01-08T17:04:16-08:00 (RFC3339占本地时区)

let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
f.timeZone = TimeZone.current
let s = f.string(from: Date())

2019-01-09 (格林尼治标准时间/祖鲁时间的标准日期戳)

let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate, .withDashSeparatorInDate]
let s = f.string(from: Date())

2019-01-08 (占本地时区的标准日期戳)

let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate, .withDashSeparatorInDate]
f.timeZone = TimeZone.current
let s = f.string(from: Date())

所有四个字符串代表完全相同的日期和时间。 RFC3339格式(和截短的日期戳)的一个主要优点是,它们可以通过标准的小于,大于运算符进行排序,从而无需在服务器日期对象和客户端日期对象之间进行转换。

答案 9 :(得分:0)

迅速5
如果要手动执行,
Date()将始终返回当前UTC时间

let today = Date()  

如果您居住,让我们在马来西亚说。现在是UTC + 8(意味着快8个小时)
您可以使用日历

来获取本地日期时间
let localDateTime = Calendar.current.date(byAdding: .hour, value: 8, today)!   

您可以使用的另一种方法是

let localDateTime = Date().addingTimeInterval(8 * 60 * 60) //because the param in seconds
相关问题