Swift - Get local date and time

▼魔方 西西 提交于 2019-12-01 02:16:34

update: Xcode 8.2.1 • Swift 3.0.2

You can also use the Date method description(with locale: Locale?) to get user's localized time description:

A string representation of the Date, using the given locale, or if the locale argument is nil, in the international format YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM represents the time zone offset in hours and minutes from UTC (for example, “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"

The method above it is not meant to use when displaying date and time to the user. It is for debugging purposes only.

When displaying local date and time (current timezone) to the user you should respect the users locale and device settings. The only thing you can control is the date and time style (short, medium, long or full). Fore more info on that you can check this post shortDateTime.

If your intent is to create a time stamp UTC for encoding purposes (iso8601) you can check this post iso8601

use NSDateFormatter, either by setting the format

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

or styles

dateFormatter.dateStyle = .NoStyle
dateFormatter.timeStyle = .MediumStyle
Mohammad Nurdin

I already found the answer.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
let dateInFormat = dateFormatter.stringFromDate(NSDate())
let expiryDate: Date = ...
let localizedDateString = DateFormatter.localizedString(from: expiryDate, dateStyle: .medium, timeStyle: .short)

"10 Sep 2017, 14:37"

To get back the most common string formats (when dealing with queries and databases):

Swift 4

2019-01-09T01:07:04Z (RFC3339 in GMT/Zulu time)

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

2019-01-08T17:04:16-08:00 (RFC3339 accounting for local time zone)

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

2019-01-09 (standard date stamp in GMT/Zulu time)

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

2019-01-08 (standard date stamp accounting for local time zone)

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

All four strings represent the exact same date and time. And don't forget that sorting these strings (in these formats) in alphabetical order puts them in chronological order, which means there is no need to store them as date objects in the database since you can sort and query chronologically using a basic a-to-z sort.

Leo's answer is pretty good. I just wanted to add a way to use it as a computed property.

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

Use it like so:

print(currentTime)

You have to use 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)
Ganesh Manickam

Swift 4

To get current date and time

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

but that will be in date type to convert date into string

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!