The code below not only prints date and time in Short, Medium, Long, and Full styles in the debugger console, but also shows a clock with a custom format
"yyyy/MM/dd\nEEEE\nhh:mm:ss a\nzzzz", which includes date, day of week, time, and timezone. For more information about the meaning of characters within the custom format string, please refer to Date Format Patterns.
1. Modify ViewController.swift as:
import UIKit
class ViewController: UIViewController {
var format : NSDateFormatter!
var labelClock : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//Light pink color with RGB values
view.backgroundColor = UIColor(red: 255.0/255.0, green: 200.0/255.0, blue: 210.0/255.0, alpha: 1.0)
let label = UILabel(frame: CGRectMake(0,0,200,100))
label.center = CGPoint(x: view.center.x, y: 150)
label.text = "Now is"
label.textAlignment = NSTextAlignment.Center //Align to center
view.addSubview(label)
labelClock = UILabel(frame: CGRectMake(0,0,250,100))
labelClock.center = view.center
labelClock.textAlignment = NSTextAlignment.Center
labelClock.numberOfLines = 0 //Multi-lines
labelClock.font = UIFont(name: "Helvetica-Bold", size: 20)
view.addSubview(labelClock)
format = NSDateFormatter()
//Print different date styles.
printDateStyles()
//Custom Format for clock
format.dateFormat = "yyyy/MM/dd\nEEEE\nhh:mm:ss a\nzzzz"
//Update the date and time of the clock periodically.
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateClock", userInfo: nil, repeats: true)
}
//Print different styles once in debugger console
func printDateStyles() {
let formatArray : [NSDateFormatterStyle] = [NSDateFormatterStyle.ShortStyle, NSDateFormatterStyle.MediumStyle, NSDateFormatterStyle.LongStyle, NSDateFormatterStyle.FullStyle]
let formatStringArray : [String] = ["No","Short","Medium","Long","Full"]
let now = NSDate()
var outputString : String!
//Show Date Styles
print("---Date Styles---")
for style in formatArray {
format.dateStyle = style
outputString = "\(formatStringArray[style.hashValue]) Style:\t\(format.stringFromDate(now))"
print(outputString)
}
//Remove Date String
format.dateStyle = NSDateFormatterStyle.NoStyle
//Show Time Styles
print("\n---Time Styles---")
for style in formatArray {
format.timeStyle = style
outputString = "\(formatStringArray[style.hashValue]) Style:\t\(format.stringFromDate(now))"
print(outputString)
}
}
//Update clock every second
func updateClock() {
let now = NSDate()
labelClock.text = format.stringFromDate(now)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2. The print result with various styles:
---Date Styles---
Short Style: 12/31/15
Medium Style: Dec 31, 2015
Long Style: December 31, 2015
Full Style: Thursday, December 31, 2015
---Time Styles---
Short Style: 11:46 PM
Medium Style: 11:46:21 PM
Long Style: 11:46:21 PM GMT+8
Full Style: 11:46:21 PM Taipei Standard Time
3. The clock result:
A few moments later... Happy New Year!!
4. Change the language setting of iPhone. The date, day of week, time, and timezone data are also changed automatically.
The results below are with Traditional Chinese. (當iPhone的語言設定改變時,日期、星期、時間和時區等資料也會跟著語言設定改變。以下為繁體中文的結果。)
5. The print result in Chinese:
---Date Styles---
Short Style: 2016/1/1
Medium Style: 2016年1月1日
Long Style: 2016年1月1日
Full Style: 2016年1月1日 星期五
---Time Styles---
Short Style: 下午10:29
Medium Style: 下午10:29:04
Long Style: GMT+8 下午10:29:04
Full Style: 台北標準時間 下午10:29:04
6. The clock result in Chinese:
No comments:
Post a Comment