Get Current Time as String Swift 3.0

Get current time as string swift 3.0

This is the way I figure it out.

   // Get today date as String

func getTodayString() -> String{

let date = Date()
let calender = Calendar.current
let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)

let year = components.year
let month = components.month
let day = components.day
let hour = components.hour
let minute = components.minute
let second = components.second

let today_string = String(year!) + "-" + String(month!) + "-" + String(day!) + " " + String(hour!) + ":" + String(minute!) + ":" + String(second!)

return today_string

}

let today : String!

today = getTodayString()

How to get the current time as datetime

Update for Swift 3:

let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)

I do this:

let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute

See the same question in objective-c How do I get hour and minutes from NSDate?

Compared to Nate’s answer, you’ll get numbers with this one, not strings… pick your choice!

Current time to string in Swift

If you just want the current date and time as a string, and you really don’t care about the time zone or the format in any way, this will do:

let nowString = "\(Date())"

Get current date in Swift 3?

You say in a comment you want to get "15.09.2016".

For this, use Date and DateFormatter:

let date = Date()
let formatter = DateFormatter()

Give the format you want to the formatter:

formatter.dateFormat = "dd.MM.yyyy"

Get the result string:

let result = formatter.string(from: date)

Set your label:

label.text = result

Result:

15.09.2016

How to get Date from String in swift 3?

Military time (24-value hours) uses the capital 'H'. Try this for your formatting String:

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"

Swift 3 - How to display time as String (each 5 minutes)

here is an extension which can help you out.

extension Date {
func years(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}
func months(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}
func weeks(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfYear], from: date, to: self).weekOfYear ?? 0
}
func days(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
func hours(from date: Date) -> Int {
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}
func minutes(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
func seconds(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}

var relativeTime: String {
let now = Date()
if now.years(from: self) > 0 {
return now.years(from: self).description + " year" + { return now.years(from: self) > 1 ? "s" : "" }() + " ago"
}
if now.months(from: self) > 0 {
return now.months(from: self).description + " month" + { return now.months(from: self) > 1 ? "s" : "" }() + " ago"
}
if now.weeks(from:self) > 0 {
return now.weeks(from: self).description + " week" + { return now.weeks(from: self) > 1 ? "s" : "" }() + " ago"
}
if now.days(from: self) > 0 {
if now.days(from:self) == 1 { return "Yesterday" }
return now.days(from: self).description + " days ago"
}
if now.hours(from: self) > 0 {
return "\(now.hours(from: self)) hour" + { return now.hours(from: self) > 1 ? "s" : "" }() + " ago"
}
if now.minutes(from: self) > 0 {
return "\(now.minutes(from: self)) minute" + { return now.minutes(from: self) > 1 ? "s" : "" }() + " ago"
}
if now.seconds(from: self) > 0 {
if now.seconds(from: self) < 15 { return "Just now" }
return "\(now.seconds(from: self)) second" + { return now.seconds(from: self) > 1 ? "s" : "" }() + " ago"
}
return ""
}
}

you can use it like this

let timePeriod = (oldestInfoDate as Date).relativeTime

If you need any help understanding it, do let me know.

How to change time format from string in Swift 3.0

let time = "22:02:00"

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "HH:mm:ss"

var fullDate = dateFormatter.date(from: time)

dateFormatter.dateFormat = "hh:mm:ss a"

var time2 = dateFormatter.string(from: fullDate!)

iOS Swift - Get the Current Local Time and Date Timestamp

For saving Current time to firebase database I use Unic Epoch Conversation:

let timestamp = NSDate().timeIntervalSince1970

and For Decoding Unix Epoch time to Date().

let myTimeInterval = TimeInterval(timestamp)
let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))


Related Topics



Leave a reply



Submit