如何将此字符串“2016-04-14T10:44:00+0000”转换为NSDate并只保留年、月、日、小时?

中间的T真的让我在处理日期时不习惯。


当前回答

只是 步骤1 > 从JSON或dataSource获取String值

步骤2 >创建本地变量并赋值。

let startDate = CurrentDashBoard.startdate

步骤3>创建DateFormatter实例。

let dateFormatter = DateFormatter()

步骤4 > 从dateFormatter调用dateFormat并提供保存的日期dataType。

dateFormatter.dateFormat = "MM-dd-yyyy HH:mm:ss"("may be String formate")

步骤5 > 将Local变量赋给要转换的变量。

let dateFromStringstartDate : NSDate = dateFormatter.date(from: startDate)! as NSDate

步骤6 > 请按以下代码填写所需日期。

dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"

步骤6>分配给label/text

cell.lblStartDate.text = String(format: "%@", strstartDate)

代码:

let startDate = CurrentDashBoard.startdate let dateFormatter = DateFormatter() 
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm:ss" let dateFromStringstartDate : 
NSDate = dateFormatter.date(from: startDate)! as NSDate
dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
let strstartDate = dateFormatter.string(from: dateFromStringstartDate as Date)

其他回答

只需要传递dateformate和date,就可以得到年,月,日,小时。额外的信息

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

斯威夫特5。看是否一个日期已经通过:

let expiryDate = "2020-01-10" // Jan 10 2020

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

if Date() < dateFormatter.date(from: expiryDate) ?? Date() {
    print("Not Yet expiryDate")
} else {
    print("expiryDate has passed")
}

从iOS 15.0开始,我们可以更快速地将字符串转换为日期:

let strategy = Date.ParseStrategy(format: "\(year: .defaultDigits)-\(month: .twoDigits)-\(day: .twoDigits)T\(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .zeroBased)):\(minute: .twoDigits):\(second: .twoDigits)\(timeZone: .iso8601(.short))", timeZone: .current)
let date = try? Date("2016-04-14T10:44:00+0000", strategy: strategy)

创建全局函数

func convertDateFormat(inputDate: String) -> String {

     let olDateFormatter = DateFormatter()
     olDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

     let oldDate = olDateFormatter.date(from: inputDate)

     let convertDateFormatter = DateFormatter()
     convertDateFormatter.dateFormat = "MMM dd yyyy h:mm a"

     return convertDateFormatter.string(from: oldDate!)
}

调用函数并在其中传递值

get_OutputStr = convertDateFormat(inputDate: "2019-03-30T05:30:00+0000")

这里是输出

Feb 25 2020 4:51 PM



 

有时,在swift中将字符串转换为日期可能会导致返回nil,因此您应该在格式中添加“!”标记。日期函数!

let dateFormatterUK = DateFormatter()
dateFormatterUK.dateFormat = "dd-MM-yyyy"

let stringDate = "11-03-2018"
let date = dateFormatterUK.date(from: stringDate)!