简体   繁体   English

从日期字符串中分别获取月份和日期

[英]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.即从上面的日期字符串,我想要 'December' 和 '16' 分开。

There are several ways to get the expected result, as an option you can use this code with Calendar :有几种方法可以获得预期的结果,作为一个选项,您可以将此代码与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斯威夫特 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我给你月份的例子你可以分别获得日期和月份值,访问你的格式的链接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.您也可以使用Calendar对象来获取日期、月份(以数字形式提供)和年份。

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)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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