How to get the name of each month and month


Actual combat demand

  • Get month
  • Gets the name of the month

The value and harvest of this paper

After reading this article, you will be able to make the following interface

How to get the name of each month and month

How to get the name of each month and month

Basic knowledge (master can skip this section, more wonderful later)

Date display and processing are skills that every swiftui developer should master. Date, calendar and dateformatter are the three most commonly used components of date processing. Let’s take a look at the use of all three of them

  • Date gets the type of time

The date type is mainly used to get the date and time, and itself provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from relative to another date. In addition, date and are used in conjunction with the dateformatter instance to create localized representations of dates and times and, together with calendar, create calendar values that perform calendar arithmetic.

  • Calendar processing date type

Calendar type encapsulates the related calculation functions of date, month and day. Through calendar, we can get the date and calculate according to the date.

  • DateFormatter

Dateformatter is used to convert a date into a text representation.


import SwiftUI

struct ContentView: View {
    @State var monthName = ""
    var body: some View {
        VStack {
            Text ("how to get the name of each month and month")
                    Text ("month of this month:)
                Button ("get month name"){
                    self.monthName = Date().getMonthName()

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

extension Date {
    func getMonthName() -> String {
        let date = self

        let month = Calendar.current.component(.month, from: date)

        let months = ["January", "February", "March", "April", "May", "June",
                      "July", "August", "September", "October", "November", "December"]

        let monthName = months[month-1]

       return monthName

Technical exchange

Swiftui technology exchange QQ group: 518696470

  • Please follow my column icloudend, swiftui tutorial and source code…

Recommended Today

Java security framework

The article is mainly divided into three parts1. The architecture and core components of spring security are as follows: (1) authentication; (2) authority interception; (3) database management; (4) authority caching; (5) custom decision making; and;2. To build and use the environment, the current popular spring boot is used to build the environment, and the actual […]