I have two iPhones, one is India (with sim) and second one is in the USA (without sim). Now I am trying to convert string to date but its not working USA Phone.
Here is my code. The function name is convertUnixTimeFromString
is going crash so I am using if condition.
let start = getdate //"2016-01-03 06:35:53"//
print("TheDate========\(start)")
let INPUT_FORMAT:String = "yyyy-MM-dd HH:mm:ss"
let str_date:NSDate = DateUnixConvertor.convertDateFromString(start, format: INPUT_FORMAT)
public class func convertUnixTimeFromString(string:String , format:String) -> Double
{
let formatter:NSDateFormatter = DateUnixConvertor.setFormatterWithFormat(format)
formatter.timeZone = NSTimeZone(name: "UTC")
//NSTimeZone(name: "UTC")
var date = NSDate();
if let startDate = formatter.dateFromString(string) {
date = startDate
}
//let date:NSDate = formatter.dateFromString(string)!
let unixTime:Double = self.convertUnixTimeFromDate(date)
return unixTime
}
public class func convertStringFromDate(date:NSDate,format:String)->String
{
let unixTime:Double = DateUnixConvertor.convertUnixTimeFromDate(date)
let dateString:String = DateUnixConvertor.convertStringFromUnixTime(unixTime, format: format)
return dateString
}
public class func setFormatterWithFormat(format:String)->NSDateFormatter
{
let formatter:NSDateFormatter = NSDateFormatter()
formatter.dateFormat = format
formatter.locale = NSLocale.currentLocale()
return formatter
}
public class func convertDateFromUnixTime(unixtime:Double) -> NSDate
{
let date:NSDate = NSDate(timeIntervalSince1970: unixtime)
return date
}
I have checked both phone zones using this code
func ltzAbbrev() -> String { return NSTimeZone.localTimeZone().abbreviation ?? "" }
var getzone:String = ltzAbbrev();
print(getzone)
Response of this code India == IST, USA == GMT +5:30 Please help me.
The abbreviations value differ based on NSLocale. Try setting NSLocale to India, so that you will get the value as IST.
Sample code:
let locale = NSLocale(localeIdentifier: "en_IN")
dafeFormatter.locale = locale