質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2732閲覧

現在日時の表示について

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/02/13 15:00

イメージ説明

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var lablenow_time: UILabel! 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 let lablenow_time = NSDate() // 現在日時の取得 11 12 let dateFormatter = DateFormatter() 13 dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 14 dateFormatter.dateFormat = "yyyy/MM/dd HH:mm:ss" // 日付フォーマットの設定 15 16 print(dateFormatter.string(from: lablenow_time as Date)) // -> 2014/06/25 02:13:18 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23 24 25} 26

自分でやってみたこと
現在日時を習得しデバッグエリアのところまで表示させることが出来た

実現したいこと
表示されているlableに現在日時を表示できるようにしたいです

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

UILabeltextプロパティに文字列を設定すれば表示されます。

swift

1let dateString = dateFormatter.string(from: lablenow_time as Date) 2print(dateString) // -> 2014/06/25 02:13:18 3self.lablenow_time.text = dateString

投稿2017/02/13 15:45

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/02/13 16:03

ありがとうございました おかげさまでイメージ通りに表示させることが出来ました。 それともう一つお聞きしたいのですがtableViewのsubtitleにもこのようにコードを書けば現在日時を表示出来るようになりますか?
_Kentarou

2017/02/13 17:03

detailTextLabelというラベルなので回答と同じ様に設定できます。 cell.detailTextLabel.text = "Subtitle"
退会済みユーザー

退会済みユーザー

2017/02/14 00:23

ありがとうございます。 やってみます。
退会済みユーザー

退会済みユーザー

2017/02/16 04:10

すいませんが差し支えがなければ教えていただきたいことがあるのですが、先日、tableviewのsubtitleについて質問し、教えていただいたコードを参考にしてみたのですがどうもデバッグエリアでしか現在日時が表示されず自分なり色々と工夫したのですがどうしてもsubtitleに現在日時を表示させることが出来ない状態にいます。 もしよろしければ、もう一度教えていただけませんか?
退会済みユーザー

退会済みユーザー

2017/02/16 04:11

コードはこちらです ↓ import UIKit import CoreData class ViewController: UIViewController, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var people = [Person]() override func viewDidLoad() { super.viewDidLoad() title = "\"The List\"" tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TestCell") } func tableView(_ tableVeiw: UITableView, numberOfRowsInSection section: Int) -> Int { return people.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell") let person = people[indexPath.row] cell!.textLabel!.text = person.value(forKey: "name") as? String //subtitle 現在日時を取得 let now = NSDate() // 現在日時の取得 let dateFormatter = DateFormatter() dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale! // ロケールの設定 dateFormatter.dateFormat = "yyyy年MM月dd日 HH:mm"//:ss" // 日付フォーマットの設定 let dateString = dateFormatter.string(from: now as Date) print(dateString) // -> 2014/06/25 02:13:18 cell!.detailTextLabel!.text = "TestCell" //cell!.detailTextLabel?.text = person.value(forKey: "Subtitle") as? String return cell! } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext if editingStyle == .delete{ //self.tableView.setEditing(true, animated: true) context.delete(people[indexPath.row]) (UIApplication.shared.delegate as! AppDelegate).saveContext() do { people = try context.fetch(Person.fetchRequest()) }catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } tableView.reloadData() } } @IBAction func addName(_ sender: Any) { let alert = UIAlertController(title: "New name", message: "Enter a new name", preferredStyle: .alert) let saveAction = UIAlertAction(title: "Save", style: .default) { (action) in let textField = alert.textFields?.first self.saveName(name: textField!.text!) self.tableView.reloadData() } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addTextField(configurationHandler: nil) alert.addAction(saveAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } func saveName(name: String){ let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext let person = Person(entity: Person.entity(), insertInto: context) person.setValue(name, forKey: "name") do{ try context.save() people.append(person) } catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext do{ let result = try context.fetch(Person.fetchRequest()) people = result as! [Person] }catch let error as NSError { print("Could not save \(error), \(error.userInfo)") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
退会済みユーザー

退会済みユーザー

2017/02/16 13:59

終わった質問にさらに質問してしまいすいませんでした 多分気にしていないと思いますが一応解決できたので念の為にご報告しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問