Created
September 3, 2020 18:21
-
-
Save carlynorama/8b3d6955059ddfaa16d48887fd188b4e to your computer and use it in GitHub Desktop.
Getting Built in Date Formats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
struct Day { | |
@State var date = Date() | |
var dateString:String { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .full | |
//formatter.timeStyle = .full | |
formatter.timeStyle = .none | |
return formatter.string(from: date) | |
} | |
var dayAbrvString:String { | |
let formatter = DateFormatter() | |
formatter.dateFormat = "EE" | |
return formatter.string(from: date) | |
} | |
var dayIndex:Int { | |
Calendar.current.component(.weekday, from: date)-1 //calendar returns a number 1-7, vs 0-6 | |
} | |
func getAbrvFromIndex(whichDay:Int) -> String { | |
let formatter = DateFormatter() | |
let weekdays = formatter.shortWeekdaySymbols //also veryShort | |
return weekdays![whichDay] | |
} | |
func getDayNameFromIndex(whichDay:Int) -> String { | |
let formatter = DateFormatter() | |
let weekdays = formatter.weekdaySymbols //also veryShort | |
return weekdays![whichDay] | |
} | |
func getVeryShortNameFromIndex(whichDay:Int) -> String { | |
let formatter = DateFormatter() | |
let weekdays = formatter.veryShortWeekdaySymbols //also veryShort | |
return weekdays![whichDay] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment