Converting String to Date is always 2 hours behind - ios

I know this is a topic with several answers and whilst maybe i missed something similar to mine, in all my effort i've looked but can't find the same thing:
I have a string of date format:
24/12/2019 10:34 AM
I am trying to convert it to Date type, but this is what i get when i print the converted date:
2019-12-24 08:34:00 +0000
It is ALWAYS 2 hours behind.
I have tried:
dateFormatter.timeZone = TimeZone(abbreviation: "CAT")
and
dateFormatter.timeZone = .current
to absolutely no avail. Also, the printed date is not formatted according to my required format
dd/MM/yyyy hh:mm a
My code is as below:
var dateFormat = "dd/MM/yyyy hh:mm a"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "CAT")
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.date(from: "24/12/2019 10:34 AM")
print(date)
Any assistance would be appreciated

Related

why Date will return nil when using correct format in swift 4?

so here is the string Date that I want to Convert
2019-03-22T00:00:00
and here is the problem
Remember that I used "yyyy-MM-dd'T'HH:mm:ss" or "yyyy-MM-ddTHH:mm:ss" or "yyyy-MM-dd HH:mm:ss" or yyyy-MM-dd'T'HH:mm:ssZ and some other similar formats
You have to set a time zone and locale when parsing internet dates:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
// necessary to avoid daylight saving (and other time shift) problems
dateFormatter.timeZone = TimeZone(secondsFromGMT: TimeZone.current.secondsFromGMT())
// necessary to avoid problems with 12h vs 24h time formatting
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
print(dateFormatter.date(from: "2019-03-22T00:00:00"))
The default time zone contains information about daylight saving time and some specific times does not exist there. Instead, we have to use a generic time zone.
See https://developer.apple.com/library/archive/qa/qa1480/_index.html
22 March 2019 around midnight is the day & time when daylight saving change in Iran makes an hour non-existent.
The same can be achieved using ISO8601DateFormatter:
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = ISO8601DateFormatter.Options.withInternetDateTime.subtracting(.withTimeZone)
print(dateFormatter.date(from: "2019-03-22T00:00:00"))
Try this function might help
public func dateFormatter(strDate: String) -> String{
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fa_IR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let date = dateFormatter.date(from: strDate)
let dateString = dateFormatter.string(from: date!)
return dateString
}
dateFormatter.isLenient = true
Ignores missing hours during daylight saving time offset

Date() returns hour over 24

I have to send date from my iPhone through my app to server, but I was surprised that the date had stored as NULL (not always) in server, that's the code used for that:
timeS = (Date().millisecondsSince1970)
// make some assertions on timeS
let date = Date(timeIntervalSince1970: timeS)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: date)
return dateString
It seems like the c# code in the server side tried to convert the string to date and it failed, so it was stored NULL.
So recently I tried to send the date without any modifications in a string to see what happened:
self.backS.sendLocation(msg: "data fitched \(Date())")
and that's what I found in the database:
data fitched 2018-05-08 77:45:44 AM +0000
it must be:
data fitched 2018-05-08 07:45:44 AM +0000 (because that was the time for the server)
I don't know why is that happened!!
I really appreciate anybody's help, thanks in advance.
Try to get your current date and time like this in Swift.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let result = formatter.string(from: date)
self.backS.sendLocation(msg: "data fetched \(result)")
Your result will be like this.
"2018-05-12 14:34:51 +0000"
To send the date to server you should rather use Unix timestamp, instead of date in string format.
But, if you insist on a string, you should use DateFormatter. When using DateFormatter you should always set the locale property of your formatter. That ensures the formateer will return consistens results, regardless of your phone's regional settings.
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let dateString = formatter.string(from: Date())
It's important to set the locale property before you set the dateFormat property.
If you don't set the locale property, the formatter might return nil when working with 24-format on devices with 12-hour format set, and vice versa.
Source: Technical Q&A QA1480

Convert 12 hour date format into 24 hour format & vice versa in swift?

I have a date from a UIControl. I get date as string such as 06-12-2016 01:25 PM. Now I want to convert it into a format as 2016-12-06 13:25:00 I have tried below code t do so but it just gives me wrong date for ex. 2016-12-06 18:55:00. I have used below code for this:
func converStingToDate(str_date:String)->String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy H:mm a"
let date = dateFormatter.date(from: str_date)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date!)
}
Please suggest me a better code. Also if you guys can help me to understand all this date formatting concept how it is done ?
The code is correct and the date is correct (although you should use h for 12-hour hour).
Your time zone is UTC+0530 and print() displays the date in UTC.
If you need to print the correct local time set the time zone of the formatter to UTC:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")`

Convert Optional string to date

I have this Optional(2015-07-27 19:29:50 +0000) as a string? and I want to convert it to a date (NSDate) but I can't figure it out I've been searching a lot for solutions but I am pretty new to coding so if anyone could help I would appreciate it.
This is what I am using to convert the string to a date currently.
note: dateString is Optional(2015-07-27 19:29:50 +0000)
dateFormatter = NSDateFormatter()
println(dateFormatter)
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.dateFromString(dateString)
I always get a nil value
If you are getting nil from NSDateFormatter, you likely specified incorrect date format. Try this:
let strTime = "2015-07-27 19:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.dateFromString(strTime) // Returns "Jul 27, 2015, 12:29 PM" PST
You will want to use a NSDateFormatter and call dateFromString: on it to get the date from the string. See Apple's NSDateFormatter documentation for a great explanation of doing what you want.
In swift 3.0:
let strTime = "2015-07-27 19:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.date(from: strTime)
dateString is already date and need to convert to string. Below code is working fine
dateFormatter = NSDateFormatter()
println(dateFormatter)
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.string(from: dateString)

Swift make NSDate from string not working as expected

I'm trying to convert a date string into a NSDate. I have this input date string 2014-10-09 17:57 and this timezone +4 or Asia/Dubai and for some reason, i get this NSDate after i execute the code below 2014-10-09 13:57:00 +0000.
let dateString = "2014-10-09 17:57"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(name: "Asia/Dubai")
println("\(formatter.dateFromString(dateString))")
I want to get a date like this 2014-10-09 17:57 +0400. What am i doing wrong? Can you help me figure it out? I need the date to make some calculations between this one and another one which is correctly formatted with timezone.
It helps to breakup compound statements. The result of the formatter is a date, seconds since the reference date (GMT), it has no concept of a format or timezone.
The println() displays the date based ion the default formatting of there description method.
If you want it display in a particular way use a date formatter to create the desired string representation.
Example:
let dateString = "2014-10-09 17:57"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(name: "Asia/Dubai")
let date = formatter.dateFromString(dateString)
println("date: \(date!)") // date: 2014-10-09 13:57:00 +0000
formatter.dateFormat = "yyyy-MM-dd HH:mm Z"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)") // formattedDateString: 2014-10-09 17:57 +0400

Resources