Mercurial > public > lazybear
diff LazyBear/Global functions/ConvertEpoch.swift @ 407:c804ce7a1560
Implementing Insider networking
author | Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com> |
---|---|
date | Sun, 06 Jun 2021 13:11:41 +0200 |
parents | 8f8d5ad3dfa0 |
children | f71761f166f2 |
line wrap: on
line diff
--- a/LazyBear/Global functions/ConvertEpoch.swift Sat Jun 05 19:05:13 2021 +0200 +++ b/LazyBear/Global functions/ConvertEpoch.swift Sun Jun 06 13:11:41 2021 +0200 @@ -7,14 +7,27 @@ import SwiftUI -// Convert Epoch time to human date -func convertEpoch(_ miliseconds: Int) -> String { +/* + Convert Epoch time (in miliseconds) to human readable date + */ +func convertEpoch(_ miliseconds: Int, _ interval: Bool) -> String { let now = Date() // Current date + // TimeInterval() function must be in seconds, not in miliseconds - let articlePublished = Date(timeIntervalSince1970: TimeInterval(miliseconds/1000)) + let convertedDate = Date(timeIntervalSince1970: TimeInterval(miliseconds/1000)) + let formatter = DateComponentsFormatter() formatter.unitsStyle = .abbreviated - let humanDate = formatter.string(from: articlePublished, to: now)! + + let dateFormatter = DateFormatter() + dateFormatter.dateStyle = .medium + + var humanDate = String() + if interval { + humanDate = formatter.string(from: convertedDate, to: now)! + } else { + humanDate = dateFormatter.string(from: convertedDate) + } return humanDate }