本文介绍了使用DateComponents创建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一周的第一天和最后一天.但是我的结果与apple的文档不符:

我也希望整天而不是16:00.

解决方案

一些观察结果:

  1. 在公历中,工作日= 1表示星期日;工作日= 2表示星期一;等等.您可以查看 calendar.maximumRange(of..weekday)以获取有效值的范围,还可以查看 calendar.weekdaySymbols 来查看这些 weekDay 值的平均值(例如,"Sun","Mon","Tue","Wed","Thu","Fri"和"Sat").

  2. 您说的是

    一个 Date 对象引用了某个时刻.因此,它不能代表整个一天".但是它可以代表午夜(在您所在时区的午夜可能在格林尼治标准时间(GMT)/UTC/祖鲁(Zulu)下午4点).

    或者,您可以返回表示时间范围的 DateInterval .

      func interval(每周工作周:整数,年份:整数)->DateInterval {让日历= Calendar.currentlet date = DateComponents(calendar:calendar,weekOfYear:week,yearForWeekOfYear:year).date!返回calendar.dateInterval(of:.weekOfYear,for:date)!} 

    然后

      let formatter = DateIntervalFormatter()formatter.dateStyle = .shortformatter.timeStyle = .short让year = Calendar.current.component(.year,来自:Date())let dateInterval = interval(ofWeek:2,in:year)打印(formatter.string(来自:dateInterval)) 

    在美国语言环境中,间隔时间为1月6日:

    在德国语言环境中,时间间隔从7号开始:

  3. 如果您要从一周的第一天开始和一周的最后一天开始,则可以执行以下操作:

      func startAndEndDate(ofweek Week:Int,in year:Int)->(日期,日期){let date = DateComponents(calendar:calendar,weekOfYear:week,yearForWeekOfYear:year).date!让lastDate = calendar.date(byAdding:.day,value:6,to:date)!返回(日期,lastDate)} 

    然后

      let formatter = DateFormatter()formatter.dateStyle = .short让year = Calendar.current.component(.year,来自:Date())let(start,end)= startAndEndDate(ofWeek:2,in:year)打印(formatter.string(从:开始),-",formatter.string(从:结束)) 

I want to get the first day and the last day of the week. But my results do not match the documentation from apple:https://developer.apple.com/documentation/foundation/nsdatecomponents/1410442-weekday

This is my function:

func startAndEndDateOfWeek(weekOfYearWithYear: (week: Int,year: Int)) -> (start: Date, end: Date) {
    var output = (start: Date.init(), end: Date.init())

    let calendar = Calendar(identifier: .gregorian)
    var firstDayComponents = DateComponents()
    firstDayComponents.weekOfYear = weekOfYearWithYear.week
    firstDayComponents.yearForWeekOfYear = weekOfYearWithYear.year
    firstDayComponents.weekday = 1

    let firstDay = calendar.date(from: firstDayComponents)

    var lastDayComponents = DateComponents()
    lastDayComponents.weekOfYear = weekOfYearWithYear.week
    lastDayComponents.yearForWeekOfYear = weekOfYearWithYear.year
    lastDayComponents.weekday = 2

    let lastDay = calendar.date(from: lastDayComponents)
    output = (start: firstDay!, end: lastDay!)
    return output
}

.weekday = 2 -> leads to the sunday and not 0.

I also want to have the entire day and not 16:00.

解决方案

A couple of observations:

  1. In the Gregorian calendar, weekday = 1 means Sunday; weekday = 2 means Monday; etc. You can look at calendar.maximumRange(of: .weekday) to get the range of valid values, and you can look at calendar.weekdaySymbols to see what these weekDay values mean (e.g. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat").

  2. You said:

    A Date object references a moment in time. So it can’t represent an "entire day". But it can represent midnight (and midnight in your time zone is likely 4pm in GMT/UTC/Zulu).

    You can, alternatively, return a DateInterval, which does represent a range of time.

    func interval(ofWeek week: Int, in year: Int) -> DateInterval {
        let calendar = Calendar.current
    
        let date = DateComponents(calendar: calendar, weekOfYear: week, yearForWeekOfYear: year).date!
        return calendar.dateInterval(of: .weekOfYear, for: date)!
    }
    

    And then

    let formatter = DateIntervalFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .short
    
    let year = Calendar.current.component(.year, from: Date())
    let dateInterval = interval(ofWeek: 2, in: year)
    print(formatter.string(from: dateInterval))
    

    In a US locale, the interval starts on January 6th:

    Whereas in a German locale, the interval starts on the 7th:

  3. If you want the start of the first day of the week and the last day of the week, you can do:

    func startAndEndDate(ofWeek week: Int, in year: Int) -> (Date, Date) {
        let date = DateComponents(calendar: calendar, weekOfYear: week, yearForWeekOfYear: year).date!
        let lastDate = calendar.date(byAdding: .day, value: 6, to: date)!
    
        return (date, lastDate)
    }
    

    And then

    let formatter = DateFormatter()
    formatter.dateStyle = .short
    
    let year = Calendar.current.component(.year, from: Date())
    let (start, end) = startAndEndDate(ofWeek: 2, in: year)
    print(formatter.string(from: start), "-", formatter.string(from: end))
    

这篇关于使用DateComponents创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:10