在Swift中将String转换为NSDate [英] Convert String to NSDate in Swift
本文介绍了在Swift中将String转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此日期转换为 NSDate
datestring = /Date(147410000000)/ //String from server response
预期输出:
12/01/2014
我试过这个。但是我没有。
I tried this. But I got nil.
let dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let date = dateFormatter.dateFromString(datestring)
return date
推荐答案
147410000000 ,我认为这是时间间隔你得到的是
来自服务器。
"147410000000", i think this the time-interval which you are getting from server.
在您的情况下,您需要修剪字符串并将其从
/日期(147410000000)/ 转换为 147410000000
In your case,you need to trim the string and convert it From /Date(147410000000)/ to 147410000000
var string : String = "1408709486" // (Put your string here)
var timeinterval : NSTimeInterval = (string as NSString).doubleValue // convert it in to NSTimeInteral
var dateFromServer = NSDate(timeIntervalSince1970:timeinterval) // you can the Date object from here
println(dateFromServer) // for My Example it will print : 2014-08-22 12:11:26 +0000
// Here i create a simple date formatter and print the string from DATE object. you can do it vise-versa.
var dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "yyyy-MM-dd"
println(dateFormater.stringFromDate(dateFromServer)) // And then i can get the string like this : 2014-08-22
查看评论部分。 Martin的评论也可以帮助您解决问题。
这篇关于在Swift中将String转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文