未分類

DateFormatterの使い方

投稿日:2020年8月2日 更新日:

Date型の日付を文字列にして表示する。

let date = Date()
import Foundation

//フォーマットを指定
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
let day1 = formatter.string(from: date)

//フォーマットは自由に変えられる
formatter.dateFormat = "yyyy/M"
let day2 = formatter.string(from: date)

//規定のdateStyleを使う
formatter.dateStyle = .medium
let day3 = formatter.string(from: date)

print(date)
print(day1)
print(day2)
print(day3)

参考:DateFormatterの使い方まとめ

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

UserDefaultsの基本的な使い方

import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa …

no image

NavigationControllerでpopで前の階層を戻った時に値を渡す

NAvigationControllerで次の階層へ行く時に値を渡す方法と異なるので注意。 ひとつ前の階層のVCで受け取る変数を宣言しておく。 現在の階層で以下を記述する。 // 一つ前のViewCo …

no image

他ファイルの変数を使う

ViewControllerで宣言した変数を、NextViewControllewで使う方法。 let test = “テスト” let viewController = ViewController …

no image

TextViewにPlaceholderを追加する簡単な方法

TextViewはTextFieldのようにPlaceholderを設定できないため、自分で実装する必要がある。 ここでは、TextViewの上にLabelを設置し、TextViewの入力判定を行い、 …

no image

StaticCellでコードから特定のセルを選択状態にしたい時

セルを選択したいタイミングに以下のコードを書く。 sectionとrowでセルを指定する。 tableView.selectRow(at: IndexPath(row: 0, section: 1), …