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



我第一次自己开发了一个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()
}
}`

我不确定我是否完全理解你在寻找什么,但似乎你说你想要选择一个日期并找出该日期周围的天数。下面是一些代码:

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))
}
}

代码以日期"October 21, 2021"为参数。并使用dateInterval(of:for:)来计算该日期周围一周中的天数。这将打印出:

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

注意:您的计算机可能会根据区域设置给出不同的日期。我在美国,美国通常认为周日是一周的第一天。据我所知,在欧洲,星期一通常是一周的第一天。所以如果你用相同的代码得到不同的日期,这可能是因为你的区域设置不同。

相关内容

  • 没有找到相关文章

最新更新