Mercurial > public > lazybear
comparison 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 |
comparison
equal
deleted
inserted
replaced
406:09d05e48462f | 407:c804ce7a1560 |
---|---|
5 // Created by Dennis Concepción Martín on 28/3/21. | 5 // Created by Dennis Concepción Martín on 28/3/21. |
6 // | 6 // |
7 | 7 |
8 import SwiftUI | 8 import SwiftUI |
9 | 9 |
10 // Convert Epoch time to human date | 10 /* |
11 func convertEpoch(_ miliseconds: Int) -> String { | 11 Convert Epoch time (in miliseconds) to human readable date |
12 */ | |
13 func convertEpoch(_ miliseconds: Int, _ interval: Bool) -> String { | |
12 let now = Date() // Current date | 14 let now = Date() // Current date |
15 | |
13 // TimeInterval() function must be in seconds, not in miliseconds | 16 // TimeInterval() function must be in seconds, not in miliseconds |
14 let articlePublished = Date(timeIntervalSince1970: TimeInterval(miliseconds/1000)) | 17 let convertedDate = Date(timeIntervalSince1970: TimeInterval(miliseconds/1000)) |
18 | |
15 let formatter = DateComponentsFormatter() | 19 let formatter = DateComponentsFormatter() |
16 formatter.unitsStyle = .abbreviated | 20 formatter.unitsStyle = .abbreviated |
17 let humanDate = formatter.string(from: articlePublished, to: now)! | 21 |
22 let dateFormatter = DateFormatter() | |
23 dateFormatter.dateStyle = .medium | |
24 | |
25 var humanDate = String() | |
26 if interval { | |
27 humanDate = formatter.string(from: convertedDate, to: now)! | |
28 } else { | |
29 humanDate = dateFormatter.string(from: convertedDate) | |
30 } | |
18 | 31 |
19 return humanDate | 32 return humanDate |
20 } | 33 } |