【问题标题】:Swift - Get local date and timeSwift - 获取本地日期和时间
【发布时间】:2015-02-09 06:43:44
【问题描述】:

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

let last_login = String(NSDate.date())

【问题讨论】:

标签: ios swift date nsdate


【解决方案1】:

更新:Xcode 8.2.1 • Swift 3.0.2

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

日期的字符串表示,使用给定的语言环境,或者如果语言环境 参数为零,采用国际格式 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

【讨论】:

  • 您好,如何将这个巨大的字符串转换为简单的日期? dd/MM/yyyy HH:mm:ss ??
  • 您是要向用户显示还是将其作为 iso8601 日期字符串发送到您的服务器?上述方法仅用于调试目的。
  • @LeoDabus 是否可以将巴西利亚夏令时显示为 BST?
  • @SamiulIslamSami 使用缩写时要小心。这是模棱两可的。 BST 也可以解释为英国夏令时。您应该始终使用标识符。
【解决方案2】:

如果你想得到一个 Date 对象 而不是字符串表示,你可以使用下面的 sn-p:

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()

【讨论】:

  • @LeoDabus 需要详细说明吗?
  • 日期只是一个时间点。它没有时区(实际上是自 2001 年 1 月 1 日 00:00:00 UTC 以来的秒数)。只有日期的字符串表示可以有时区
  • 来自文档 日期值封装了一个时间点,独立于任何特定的日历系统或时区。日期值表示相对于绝对参考日期的时间间隔。
  • 如果你想理解我的意思,试试let localDate = Date().localDate() 然后print(localDate.description(with: .current)) 看看结果
  • 这个答案是处理日期的错误方法。不要这样做。正如 Leo Dabus 在他的评论中所说,一个 Date 对象捕获一个时间点,独立于时区,并且将日期强制为显示正确 UTC 时间的不同日期是完全错误的。
【解决方案3】:

Leo 的回答很棒。我只是想添加一种将其用作计算属性的方法。

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

像这样使用它:

print(currentTime)

或者你可以封装它:

extension String { 
    static var currentTime: String { 
        Date().description(with: .current) 
    }
}

然后你可以在任何你使用字符串的地方使用它:

var time: String = .currentTime

【讨论】:

  • 要这样调用它,您需要将变量 currentTime 声明为静态。
【解决方案4】:

通过设置格式来使用 NSDateFormatter

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

或样式

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

【讨论】:

    【解决方案5】:

    我已经找到答案了。

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

    【讨论】:

    • 如何获取 Date 对象而不是字符串
    【解决方案6】:
    let expiryDate: Date = ...
    let localizedDateString = DateFormatter.localizedString(from: expiryDate, dateStyle: .medium, timeStyle: .short)
    

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

    【讨论】:

      【解决方案7】:

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

      斯威夫特 4、5

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

      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())
      

      所有四个字符串都代表完全相同的时间点。请记住,按字母顺序对这些字符串进行排序也会按时间顺序对它们进行排序,这使得这个数据数据库不可知(我一直致力于)。

      【讨论】:

        【解决方案8】:

        你必须使用 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)
        

        【讨论】:

          【解决方案9】:

          斯威夫特 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
          

          【讨论】:

          • dateFormatter.dateFormate? dd-MM-yyy HH:mm?
          猜你喜欢
          • 2018-03-04
          • 1970-01-01
          • 2021-01-02
          • 1970-01-01
          • 2016-02-07
          • 2014-02-22
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多