我如何找到下一个周末的斯威夫特 [英] How can I find the next weekend Swift

查看:91
本文介绍了我如何找到下一个周末的斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要在星期六或星期日显示一些数据的应用程序. 实际上,我有一个分段控件,如果我按一个选项(周末),我想检查是星期六还是星期日,但只有第一个周末.

I have an app in which I want to show some data when it's Saturday or Sunday. Actually I have a segmented control and if I press one of my option (which will be the weekend) I want to check If it is Saturday or Sunday but only the first weekend.

这是我为分段控件中的第一个选项所做的以获取当前日期所做的事情

This is what I've done for my first option in segmented control to take the current date

dateevent是一个变量,我需要检查该变量是否为currentDate 将currentDate声明为currentDate

dateevent is a variable that I take to check if is the currentDate currentDate is declared to be the currentDate

  if  dateevent.earlierDate(self.currentDate).isEqualToDate(self.currentDate){

  if NSCalendar.currentCalendar().isDate(dateevent, equalToDate: self.currentDate, toUnitGranularity: .Day){

                                //Do something
                            }

                        }

推荐答案

首先找到要添加到NSDateComponents weekday属性的天数,然后可以使用dateByAddingComponents(_:toDate:options:).

First find the number of days to add to NSDateComponents weekday property and then You can use dateByAddingComponents(_:toDate:options:).

let today = NSDate()
let calendar = NSCalendar.currentCalendar()

let todayWeekday = calendar.component(.Weekday, fromDate: today)

let addWeekdays = 7 - todayWeekday  // 7: Saturday number
var components = NSDateComponents()
components.weekday = addWeekdays

let nextSaturday = calendar.dateByAddingComponents(components, toDate: today, options: .MatchFirst)

这篇关于我如何找到下一个周末的斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆