2016-04-26 245 views
11

如何將此字符串「2016-04-14T10:44:00 + 0000」轉換爲NSDate表單並僅保留年,月,日,小時?Swift將字符串轉換爲日期

在它的中間的T真的拋出了我用workinf時的習慣。

親切的問候

+0

你只想年,月,日,小時吧? –

+0

可能重複的[從字符串到NSDate在Swift](http://stackoverflow.com/questions/25686284/from-string-to-nsdate-in-swift) – Fogmeister

回答

41
  • 轉換的ISO8601刺痛最新

    let isoDate = "2016-04-14T10:44:00+0000" 
    
    let dateFormatter = NSDateFormatter() 
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" 
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") 
    let date = dateFormatter.dateFromString(isoDate)! 
    
  • 取得日期年,月,日,時mponents之日起

    let units : NSCalendarUnit = [.Year, .Month, .Day, .Hour] 
    let calendar = NSCalendar.currentCalendar() 
    let components = calendar.components(units, fromDate: date)! 
    
  • 最後創建一個新的NSDate對象和帶分秒

    let finalDate = calendar.dateFromComponents(components) 
    

斯威夫特3:

let isoDate = "2016-04-14T10:44:00+0000" 

let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" 
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX 
let date = dateFormatter.date(from:isoDate)! 
let calendar = Calendar.current 
let components = calendar.dateComponents([.year, .month, .day, .hour], from: date) 
let finalDate = calendar.date(from:components) 

還考慮在iOS的10/12的MacOS推出了方便格式化ISO8601DateFormatter

let isoDate = "2016-04-14T10:44:00+0000" 

let dateFormatter = ISO8601DateFormatter() 
let date = dateFormatter.date(from:isoDate)! 
+1

在「yyyy-MM-dd'T'HH:mm:ssZ」中的Z解決了我不適用於我的問題 – asheyla

+0

我只想要年月和日,但這不適合我 – Sneha

+0

@Sneha如果你不喜歡'不想要小時只是省略'小時' – vadian

3

請嘗試以下日期格式。

let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ" 
let date = dateFormatter. dateFromString (strDate) 

希望它可以幫助..

+0

試過了,並且日期之後返回零。 – asheyla

+1

我測試過了。現在它肯定會工作。 @asheyla –

2

嗨你有獨立的T格式,然後轉換爲你喜歡

// create dateFormatter with UTC time format 
let dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
dateFormatter.timeZone = NSTimeZone(name: "UTC") 
let date = dateFormatter.dateFromString("2015-04-01T11:42:00") 

// change to a readable time format and change to local time zone 
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a" 
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!) 
1

只要你通過你的dateformate和你約會,然後你得到Year,month,day,hourExtra info

func GetOnlyDateMonthYearFromFullDate(currentDateFormate:NSString , conVertFormate:NSString , convertDate:NSString) -> NSString 
    { 
     let dateFormatter = NSDateFormatter() 
     dateFormatter.dateFormat = currentDateFormate as String 
     let formatter = NSDateFormatter() 
     formatter.dateFormat = Key_DATE_FORMATE as String 
     let finalDate = formatter.dateFromString(convertDate as String) 
     formatter.dateFormat = conVertFormate as String 
     let dateString = formatter.stringFromDate(finalDate!) 

     return dateString 
    } 


獲得年度

let Year = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "YYYY", convertDate: "2016-04-14T10:44:00+0000") as String 


獲取月

let month = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "MM", convertDate: "2016-04-14T10:44:00+0000") as String 


獲取日

let day = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "dd", convertDate: "2016-04-14T10:44:00+0000") as String 


獲取小時

let hour = self.GetOnlyDateMonthYearFromFullDate("yyyy-MM-dd'T'HH:mm:ssZ", conVertFormate: "hh", convertDate: "2016-04-14T10:44:00+0000") as String