Created
November 19, 2016 17:50
-
-
Save takoikatakotako/34fec1ee38a09955862647ec078e94a9 to your computer and use it in GitHub Desktop.
現在の時間、曜日を取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController{ | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//現在の時間を取得 | |
let date = Date() | |
//取得する要素を選択する | |
let calendar = Calendar.current | |
let component = (calendar as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second, NSCalendar.Unit.weekday], from: date) | |
// 日時と時間の出力 | |
print("西暦:"+String(component.year!)) | |
print("月:"+String(component.month!)) | |
print("日:"+String(component.day!)) | |
print("時:"+String(component.hour!)) | |
print("分:"+String(component.minute!)) | |
print("秒:"+String(component.second!)) | |
//曜日の出力 | |
let weeksAry: Array = ["nil","Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat.","Sun."] | |
let weekDayInt:Int = component.weekday! | |
print("曜日"+weeksAry[weekDayInt]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment