How do I get the day of the week as a string?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
I needed a simple (Gregorian) day of the week index, where 0=Sunday and 6=Saturday to be used in pattern match algorithms. From there it is a simple matter of looking up the day name from an array using the index. Here is what I came up with that doesn't require date formatters, or NSCalendar or date component manipulation:
Here is the updated code for Swift 3
Code :
To Print the name of the event as String:
Vladimir's answer worked well for me, but I thought that I would post the Unicode link for the date format strings.
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
This link is for iOS 6. The other versions of iOS have different standards which can be found in the X-Code documentation.
I had quite strange issue with getting a day of week. Only setting firstWeekday wasn't enough. It was also necesarry to set the time zone. My working solution was:
outputs current day of week as a string in locale dependent on current regional settings.
To get just a week day number you must use NSCalendar class:
Here's how you do it in Swift 3, and get a localised day name…