为了给自己工作时间统计,做一个简单的考勤程序会很有效率。
在这个简单的App中,使用两个Label用于显示:
@IBOutlet weak var currentTimeLb: UILabel! //用于显示当前时间
@IBOutlet weak var recordTimeLb: UILabel! //用于显示时间差
除此外,使用两个按钮,第一个是上班按钮,按下存储上班时间;另一个是下班按钮,按下计算时间差。
在viewDidLoad中设定时间显示格式,并设定在currentTimeLb中显示当前时间,调用addCycleTimer()函数实现走秒。
override func viewDidLoad() {
super.viewDidLoad()
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//初始显示系统当前时间
let date = NSDate()
let strCurrentTime = dateFormatter.string(from: date as Date) as String
currentTimeLb.text = strCurrentTime;
//走秒,继续显示
addCycleTimer()
}
走秒函数实现如下:
//调用走秒程序
fileprivate func addCycleTimer() {
let timer = Timer(timeInterval: 1.0, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode:RunLoopMode.commonModes)
}
以及:
//走秒程序
@objc private func handleTimer (){
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let currentDate = Date(timeIntervalSinceNow: 1)
currentTimeLb.text = dateFormatter.string(from: currentDate)
}
以下对两个按钮进行编程:
首先定义公共变量,firstDate用于存储上班时间,secondDate用于存储下班时间,格式均为int。这两个变量作为公共变量在按钮函数外声明。
上班按钮用于存储firstDate变量,通过调用Label中显示的时间String转换成Date格式,调用其中从开始到现在的秒数并强转成Int,实现语句如下:
firstDate = Int(dateFormatter.date(from: dateString!)!.timeIntervalSince1970)
下班按钮中,首先用相同方式存储下班时间secondDate。由于两个函数已经得到各自秒数,所以在该按钮中做差,可以得到两个时间差(秒数):
let timeDiff=secondDate-firstDate
得到时间差后,调用显示函数getStringFromTime()将秒数转换成String格式用于显示在recordTimeLb:
recordTimeLb.text = self.getStringFromTime(seconds: timeDiff)
显示函数实现如下:
func getStringFromTime(seconds:Int) -> String {
let str_hour = NSString(format: "%02ld", seconds/3600)
let str_minute = NSString(format: "%02ld", (seconds%3600)/60)
let str_second = NSString(format: "%02ld", seconds%60)
let format_time = NSString(format: "您工作了%@小时%@分钟%@秒",str_hour,str_minute,str_second)
return format_time as String
}
其中可以通过显示格式调整最后显示的内容:
let format_time = NSString(format: "您工作了%@小时%@分钟%@秒",str_hour,str_minute,str_second)
终上,整个代码如下:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var currentTimeLb: UILabel! //用于显示当前时间
@IBOutlet weak var recordTimeLb: UILabel! //用于显示时间差
override func viewDidLoad() {
super.viewDidLoad()
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//初始显示系统当前时间
let date = NSDate()
let strCurrentTime = dateFormatter.string(from: date as Date) as String
currentTimeLb.text = strCurrentTime;
//走秒,继续显示
addCycleTimer()
}
//调用走秒程序
fileprivate func addCycleTimer() {
let timer = Timer(timeInterval: 1.0, target: self, selector: #selector(self.handleTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode:RunLoopMode.commonModes)
}
//走秒程序
@objc private func handleTimer (){
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let currentDate = Date(timeIntervalSinceNow: 1)
currentTimeLb.text = dateFormatter.string(from: currentDate)
}
func getStringFromTime(seconds:Int) -> String {
let str_hour = NSString(format: "%02ld", seconds/3600)
let str_minute = NSString(format: "%02ld", (seconds%3600)/60)
let str_second = NSString(format: "%02ld", seconds%60)
let format_time = NSString(format: "您工作了%@小时%@分钟%@秒",str_hour,str_minute,str_second)
return format_time as String
}
var firstDate = 0
var secondDate = 0
@IBAction func btnGetCurrTime(_ sender: UIButton) {
recordTimeLb.text=currentTimeLb.text
//记录上班时间
let dateString = currentTimeLb.text
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//定义变量
firstDate = Int(dateFormatter.date(from: dateString!)!.timeIntervalSince1970)
}
@IBAction func btnCalcTime(_ sender: Any) {
//记录下班时间
let dateString = currentTimeLb.text
//显示格式
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
//下班时间转为秒数
secondDate = Int(dateFormatter.date(from: dateString!)!.timeIntervalSince1970)
let timeDiff=secondDate-firstDate
recordTimeLb.text = self.getStringFromTime(seconds: timeDiff)
}
}