如何將此字符串「2016-04-14T10:44:00 + 0000」轉換爲NSDate表單並僅保留年,月,日,小時?Swift將字符串轉換爲日期
在它的中間的T真的拋出了我用workinf時的習慣。
親切的問候
如何將此字符串「2016-04-14T10:44:00 + 0000」轉換爲NSDate表單並僅保留年,月,日,小時?Swift將字符串轉換爲日期
在它的中間的T真的拋出了我用workinf時的習慣。
親切的問候
轉換的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)!
請嘗試以下日期格式。
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter. dateFromString (strDate)
希望它可以幫助..
試過了,並且日期之後返回零。 – asheyla
我測試過了。現在它肯定會工作。 @asheyla –
嗨你有獨立的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!)
只要你通過你的dateformate
和你約會,然後你得到Year,month,day,hour
。 Extra 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
你只想年,月,日,小時吧? –
可能重複的[從字符串到NSDate在Swift](http://stackoverflow.com/questions/25686284/from-string-to-nsdate-in-swift) – Fogmeister