Skip to content

Instantly share code, notes, and snippets.

@dsmailes
Last active April 1, 2023 10:31
Show Gist options
  • Save dsmailes/b2a9d75b0741754c8ecba4d0cbde701c to your computer and use it in GitHub Desktop.
Save dsmailes/b2a9d75b0741754c8ecba4d0cbde701c to your computer and use it in GitHub Desktop.
Current Day and Month calendar style view for SwiftUI with Date() extension to retrieve the values as strings.
struct DayMonthView: View {
@State var monthString = Date().getMonthString()
@State var dayString = Date().getDayString()
var body: some View {
VStack {
Text(monthString)
.foregroundColor(Color.red)
Text(dayString)
.font(.system(size: 64))
.minimumScaleFactor(0.01)
} // vstack
.frame(width: 128, height: 128, alignment: .center)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(Color.black, lineWidth: 4)
)
}
}
extension Date {
func getMonthString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM"
let month = dateFormatter.string(from: Date())
return month.uppercased()
}
func getDayString() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd"
let day = dateFormatter.string(from: Date())
return day
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment