swift实现一个简单的考勤App

本文介绍了一款简易考勤应用程序的开发过程,该程序能够记录员工的上下班时间,并计算实际工作时长。通过Swift语言实现,利用Label显示时间和计算时间差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了给自己工作时间统计,做一个简单的考勤程序会很有效率。

在这个简单的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)
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值