如何从 Swift 中特定工作日的日期范围中查找日期?

Posted

技术标签:

【中文标题】如何从 Swift 中特定工作日的日期范围中查找日期?【英文标题】:How to find the dates from a date range for a specific weekday in Swift? 【发布时间】:2021-10-23 17:36:36 【问题描述】:

我第一次自己构建了一个 ios 应用程序。这是一个工资计算应用程序。我不想在文本字段中输入总工作日数,而是想选择一个期间的开始日期和结束日期,选择我的休息日,选择公共假期日期,我想得到总的基本工作日减去休息日和公共假期为我计算。如何获取日期范围内选定日期的日期列表/数组?我已经附上了我的应用程序的代码。 `

import SwiftUI

struct ContentView: View 
    @State private var basicDays = ""
    @State private var lateEntry = ""
    @State private var absentDays = ""
    @State private var otHours = ""
    @State private var midNights = ""
    @State private var nightShifts = ""
    @State private var medAllowance = ""
    @State private var dayOffOTHours = ""
    @State private var pHolidayBasicHours = ""
    @State private var pHolidayOTHours = ""
    
    var netWage: Double 
        let monthlyBasic = 1200.00
        let dailyRate = monthlyBasic / 26
        let hourlyRate = dailyRate / 8
        let otRate = hourlyRate * 1.5
        let dayOffRate = hourlyRate * 2
        let publicHolidayRate = hourlyRate * 2
        let publicHolidayOTRate = hourlyRate * 3
        
        let basicDaysAmount = dailyRate * (Double(basicDays) ?? 0)
        let lateEntryAmount = hourlyRate * (Double(lateEntry) ?? 0)
        let absentDaysAmount = dailyRate * (Double(absentDays) ?? 0)
        let midNightAmount = 10 * (Double(midNights) ?? 0)
        let nightShiftAmount = 15 * (Double(nightShifts) ?? 0)
        let dayOffOTAmount = dayOffRate * (Double(dayOffOTHours) ?? 0)
        let publicHolidayAmount = (publicHolidayRate * (Double(pHolidayBasicHours) ?? 0)) + (publicHolidayOTRate * (Double(pHolidayOTHours) ?? 0))
        
        let totalOTAmount = otRate * (Double(otHours) ?? 0)
        let holidayOTAmount = dayOffOTAmount + publicHolidayAmount
        let otAmount = totalOTAmount + holidayOTAmount
        
        let totalDeduction = lateEntryAmount + absentDaysAmount
        let allowances = midNightAmount + nightShiftAmount
        
        let totalWage = basicDaysAmount + allowances + otAmount
        let netWage = totalWage - totalDeduction
        
        return netWage
    
    
    var body: some View 
        NavigationView 
            Form 
                Group 
                    Section(header: Text("Enter number of basic days")) 
                        TextField("", text: $basicDays)
                            .keyboardType(.numberPad)
                    
                        
                    Section(header: Text("Enter late entries in hours")) 
                        TextField("", text: $lateEntry)
                            .keyboardType(.decimalPad)
                    
                    
                    Section(header: Text("Enter number of days absent")) 
                        TextField("", text: $absentDays)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter number of OT hours worked")) 
                        TextField("", text: $otHours)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter number of Mid nights worked")) 
                        TextField("", text: $midNights)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter number of night shifts worked")) 
                        TextField("", text: $nightShifts)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter medical bill amount")) 
                        TextField("", text: $medAllowance)
                            .keyboardType(.decimalPad)
                    
                    
                    Section(header: Text("Enter number of hours worked on days-off")) 
                        TextField("", text: $dayOffOTHours)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter number of public holidays worked in total hours")) 
                        TextField("", text: $pHolidayBasicHours)
                            .keyboardType(.numberPad)
                    
                    
                    Section(header: Text("Enter number of OT hours worked on public holidays")) 
                        TextField("", text: $pHolidayOTHours)
                            .keyboardType(.numberPad)
                    
                
                
                Section

                Section 
                    Text("\(netWage, specifier: "%.2f")")
                
            
            .navigationBarTitle("My Wage")
        
    


struct ContentView_Previews: PreviewProvider 
    static var previews: some View 
        ContentView()
    
`

【问题讨论】:

看看enumerateDates(startingAfter:matching:matchingPolicy:repeatedTimePolicy:direction:using:) of Calendar 【参考方案1】:

我不确定我是否完全理解您要查找的内容,但您似乎是在说您想选择一个日期并找出该日期前后的哪几天。这是一些代码:

import Foundation

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium

if let date = dateFormatter.date(from: "October 21, 2021") 
    let calendar = Calendar(identifier: .gregorian)

    let datesInWeek = calendar.dateInterval(of: .weekOfYear, for: date)
    if let datesInWeek = datesInWeek 
        print("week from:")
        print(dateFormatter.string(from: datesInWeek.start))
        print("to:")
        print(dateFormatter.string(from: datesInWeek.end))
    

代码采用日期“2021 年 10 月 21 日”并使用 dateInterval(of:for:) 计算该日期周围一周的天数。这将打印出来:

week from: 
Oct 17, 2021 
to: 
Oct 24, 2021

注意:您的计算机可能会根据区域设置提供不同的日期。我在美国,美国通常认为星期天是一周的第一天。据我了解,在欧洲,星期一通常是一周的第一天。因此,如果您使用相同的代码得到不同的日期,可能是因为您的语言环境不同。

【讨论】:

"I want to select start date and end date of a period"。您的帖子与所询问的内容有什么关系? "How do I get a list/array of dates for a selected day within a date range?" 我的回复返回包含特定日期的时段(在本例中为“一周”时段)的开始日期和结束日期。

以上是关于如何从 Swift 中特定工作日的日期范围中查找日期?的主要内容,如果未能解决你的问题,请参考以下文章

选择日期范围内的特定日期

如何从日期中查找整个日期列的工作日

从 Vava 到 MongoDB 查询:查找特定日期范围内的事件

如何按特定日期范围(例如小时、日、月)对数据进行分组?

如何从日期范围查询中查找表中的一组缺失日期

我在表单中使用了日期范围选择器如何禁用从 MySQL 数据库中获取的特定日期范围