如何将此字符串“2016-04-14T10:44:00+0000”转换为NSDate并只保留年、月、日、小时?
中间的T真的让我在处理日期时不习惯。
如何将此字符串“2016-04-14T10:44:00+0000”转换为NSDate并只保留年、月、日、小时?
中间的T真的让我在处理日期时不习惯。
当前回答
嗨,你有单独的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!)
其他回答
嗨,你有单独的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!)
斯威夫特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")
}
试试下面的日期格式。
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter. dateFromString (strDate)
希望能有所帮助。
Swift 4.1:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter.date(from: strDate)
请使用ISO8601解析库来执行此操作。编码字符串的方法太多了。不要依赖于特定的格式,也不要依赖于始终发送相同的服务器。问题从末尾的“Z”开始,它将扩展到标准的所有品种。解析库将处理所有情况,并始终提供安全转换——而固定格式化字符串在将来可能会失败。
您可以使用这些库之一。CococaPods上也有:
https://github.com/boredzo/iso-8601-date-formatter/
https://github.com/malcommac/SwiftDate
看一下实现。它们都有几百行——这是有原因的。
关于这个问题:你可以使用NSDateComponents从日期中提取日期组件。网站上的例子正好符合你的情况。
https://developer.apple.com/documentation/foundation/nscalendar/1414841-components?language=objc
请注意,转换日期时会考虑到时区。你可能想显式地设置NSCalendar的locale。
只是 步骤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)