-1

Here is my code:

The variables:

let date = Date()
let calendar = Calendar.current

let hour = calendar.component(.hour, from:date)

let minute = calendar.component(.minute, from:date)

let second = calendar.component(.second, from:date)

Then this is where I display it in a label:

time.text = "\(hour):\(minute):\(second)"

It is showing up as 19:1, not sure if that is 24hr or UTC or whatever.

First of all, how do I get it to display: 19:01, and second, how do I make it display it is 12 hour time, so 7:01 ?

Thanks in advance!

reecefox
  • 47
  • 1
  • 1
  • 5
  • 2
    You should use DateFormatter timeStyle – Leo Dabus Jan 23 '17 at 03:15
  • 1
    http://stackoverflow.com/questions/28332946/nsdateformatter-stringfromdatensdate-returns-empty-string/28347285?s=1|0.1741#28347285 – Leo Dabus Jan 23 '17 at 03:16
  • @LeoDabus not exactly sure how I would implement that, haha. (coming from a noob) – reecefox Jan 23 '17 at 03:32
  • just add a new Swift file to your project, make sure you include the import UIKit statement to it and drop the extensions and initializers there. Then just use it anywhere in your code – Leo Dabus Jan 23 '17 at 03:33
  • for am/pm better to leave it to the user to set its locale/region preferences. You should just choose the style .short, .medium, .long or .full – Leo Dabus Jan 23 '17 at 03:34
  • @LeoDabus Ok I have so far: created the file, imported UIkit, but now, what do I put after : time.text = – reecefox Jan 23 '17 at 03:54
  • `time.text = Date().shortTime` you probably want .medium. Just create mediumTime also using my code as reference – Leo Dabus Jan 23 '17 at 03:54
  • 1
    @LeoDabus Wow, this worked like a charm, but I am also curious on how to make it update itself everytime the time changes and also, is this code going to make it local time for everyone using it ? Thanks so much by the way... – reecefox Jan 23 '17 at 03:59
  • If you need to make it update you will need to add a timer with a selector to update the UILabel. just set the timer timeInterval to 1 second – Leo Dabus Jan 23 '17 at 04:02
  • @LeoDabus Would you mind sharing the code for that as well? – reecefox Jan 23 '17 at 04:05
  • @reecefox check my answer – Ganesh Kumar Jan 23 '17 at 06:02
  • @reecefox I have posted how to add a timer to your label and also a sample project so you can check it out how to implement a Label that self updates it each second. http://stackoverflow.com/a/41817475/2303865 – Leo Dabus Jan 23 '17 at 23:25
  • @LeoDabus why would you update my post saying I am using your answer? That is absolutely ridiculous! – reecefox Jan 24 '17 at 00:04
  • @reecefox that's how SO works. you posted my code to your question you need to add the credits and source of it. – Leo Dabus Jan 24 '17 at 00:10
  • @LeoDabus OHHH, my bad! Hahaha sorry, I forget things sometimes, please reedit it in for me, my bad. – reecefox Jan 24 '17 at 00:11
  • Check my answer to your question about the timer – Leo Dabus Jan 24 '17 at 00:11

1 Answers1

0
var formatter: DateFormatter = {
        var tmpFormatter = DateFormatter()
        tmpFormatter.dateFormat = "dd-MM-YYYY hh:mm a"
        return tmpFormatter
      }()

func getDate() {
  let date = Date()
  print(formatter..string(from: date))
}
Ganesh Kumar
  • 1,631
  • 2
  • 21
  • 35