user5289721
user5289721

Reputation: 65

Check the weekday in Swift

I have a nice idea of an app and I am new in swift. The app is about a week schedule, you can add some things you have to do in a schedule.For example, on Monday you have to clean the bathroom and get milk. You can add this two things to the day plan for Monday. But my really problem is that I want the app to show the schedule of the current day when you open it. I don't have any idea how to check the day of the week so the schedule when you open the app is for the current day. It will be easy if I add a label named dayname on the top and it should show the name of the current weekday !!

Upvotes: -1

Views: 391

Answers (1)

Anina
Anina

Reputation: 453

I did not try it, but this post seems to be clear with enough details and examples dealing with dates in Swift. Check it HERE

here is an example from the same source working with NSDateComponents weekday property:

// First Saturday of March 2015, US/Eastern
let firstSaturdayMarch2015DateComponents = NSDateComponents()
firstSaturdayMarch2015DateComponents.year = 2015
firstSaturdayMarch2015DateComponents.month = 3
firstSaturdayMarch2015DateComponents.weekday = 7
firstSaturdayMarch2015DateComponents.weekdayOrdinal = 1
firstSaturdayMarch2015DateComponents.hour = 11
firstSaturdayMarch2015DateComponents.minute = 0
firstSaturdayMarch2015DateComponents.timeZone = NSTimeZone(name: "US/Eastern")
// On my system (US/Eastern time zone), the result for the line below is
// "Mar 7, 2015, 11:00 AM"
let firstSaturdayMarch2015Date = userCalendar.dateFromComponents(firstSaturdayMarch2015DateComponents)!

It says :

  • NSDateComponents‘ weekday property lets you specify a weekday numerically. In Cocoa’s Gregorian calendar, the first day is Sunday, and is represented by the value 1. Monday is represented by 2, Tuesday is represented by 3, all the way to Saturday, which is represented by 7.

Upvotes: 1

Related Questions