简体   繁体   中英

Get month and date separately from date string

I have got a date in this format..

2019-12-16 18:30:00 +0000

This is the code I have for that..

 var utcTime = "\(dic["dueDate"]!)"
 self.dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
 self.dateFormatter.locale = Locale(identifier: "en_US")
 let date = self.dateFormatter.date(from:utcTime)!
 print(date)

I wanted to extract month and date from this string. ie from the above date string, I want 'December' & '16' separately.

There are several ways to get the expected result, as an option you can use this code with Calendar :

let utcTime = "2020-01-17T22:01:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en_US")

if let date = dateFormatter.date(from:utcTime) {
    let monthInt = Calendar.current.component(.month, from: date)
    let dayInt = Calendar.current.component(.day, from: date)
    let monthStr = Calendar.current.monthSymbols[monthInt-1]
    print(monthStr, dayInt)
}

Welcome to stack overflow. You can try this :

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

Hope it helps...

Swift 5

Here is the extension you need It returns tuple having Month and date as you wanted to have

extension Date {
    func getMonthAndDate() ->(month:String , day:String) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM"

        let month = dateFormatter.string(from: self)

        dateFormatter.dateFormat = "dd"

        let day = dateFormatter.string(from: self)

        return (month,day)
    }
}

Welcome to stack overflow. Please try this.

func getMonthAndDate(dateString: String) ->(month:String , day:String) {

        guard let date = Date.getMonthAndDate(from: dateString, with: "yyyy-MM-dd'T'HH:mm:ss") else {
            return ("","")
        }
             let dateFormatter = DateFormatter()
             dateFormatter.dateFormat = "MMMM"

             let month = dateFormatter.string(from: date)

             dateFormatter.dateFormat = "dd"

             let day = dateFormatter.string(from: date)

             return (month,day)
         }

extension Date {
    static func getMonthAndDate(from str: String, with formatter: String) -> Date? {
           let dateFormatter = DateFormatter()
           dateFormatter.timeZone = TimeZone.current//(abbreviation: "GMT") //Set timezone that you want
           dateFormatter.locale = NSLocale.current
           dateFormatter.dateFormat = formatter //Specify your format that you want
           return dateFormatter.date(from: str)
       }
}

I give you example of month u can get date and month value separately , visit link for your format http://userguide.icu-project.org/formatparse/datetime

   extension Date {
        var month: String {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MMMM"
            return dateFormatter.string(from: self)
        }    
    }

you can use it in this way:

let date = Date()
 let monthString = date.month

try same thing for date, I hope it will work for you... :)

this is an example from your code. I have stored month and day in separate string to show you. You can change according to your requirements.

var utcTime = "2019-12-16 18:30:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
dateFormatter.locale = Locale(identifier: "en_US")
let date = dateFormatter.date(from:utcTime)!
print(date) //2019-12-16 18:30:00 +0000

dateFormatter.dateFormat = "MMMM"
let strMonth = dateFormatter.string(from: date)
print(strMonth) //December

dateFormatter.dateFormat = "dd"
let strDay = dateFormatter.string(from: date)
print(strDay) //16

Also you can use Calendar object to get date, month (gives you in digit) and year.

var utcTime = "2019-12-16 18:30:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss z"
dateFormatter.locale = Locale(identifier: "en_US")
let date = dateFormatter.date(from:utcTime)!

let calendarDate = Calendar.current.dateComponents([.day, .year, .month], from: date)

let day = calendarDate.day
print(day) //16
let month = calendarDate.month
print(month) //12
let year = calendarDate.year
print(year) //2019

You can get the day, month and year as follows

let yourDate = Calendar.current.dateComponents([.day, .year, .month], from: Date())
if let day = yourDate.day, let month = yourDate.month, let year = yourDate.year {
   let monthName = Calendar.current.monthSymbols[month - 1]
   // your code here
}
extension String {
  func getMonthDay() -> (Int,Int) {
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
      let date =  dateFormatter.date(from: self) ?? Date()
      let calendar = Calendar.current
      let month = calendar.component(.month, from: date)
      let day = calendar.component(.day, from: date)

      return (month, day)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM