Converting time from GMT in iOS behaving oddly (Reverse) - ios

So I am trying to convert some times I receive in UTC/GMT to the local time zone using the following code:
let gmtFormatter: NSDateFormatter = NSDateFormatter()
gmtFormatter.timeZone = NSTimeZone(name: "GMT")
gmtFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let gmtDate: NSDate = gmtFormatter.dateFromString(dateString)!
print("GMT \(gmtDate)")
let localFormatter: NSDateFormatter = NSDateFormatter()
localFormatter.timeZone = NSTimeZone.localTimeZone()
localFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let localDate: NSDate = localFormatter.dateFromString(gmtFormatter.stringFromDate(gmtDate))!
print("Local \(localDate)")
However, I find that the conversion is working in revers. PST is UTC-8 and the outputs are 8 hours ahead instead of 8 hours behind. So I tested with a London Time Zone (CET) which is UTC+1 and should be 1 hour ahead of GMT. The results was a time that was 1 hour behind.
This was the string I am testing with: "2016-02-24 00:05:54"
For PST this should convert to 2016-02-23 04:05:54, but instead converts to 2016-02-24 08:05:54 (notice the way it should be is on the 23rd and not the 24th)
For CET this should convert to 2016-02-24 01:05:54, but instead converts to 2016-02-23 23:05:54 (notice the way it should be is on the 24th and not the 23rd)
Am I missing something glaring?

The problem is that in the second part of your code...
let localDate: NSDate =
localFormatter.dateFromString(gmtFormatter.stringFromDate(gmtDate))!
... you're doing the opposite of what you want. An NSDate is absolute. Its string representation is what is volatile, as it were. So having turned a string into a date with your first formatter, what you now want to do is turn that date into a string with your second formatter.
For example, I'm in California. Now, watch this:
let dateString = "2016-02-24 10:09:08"
let gmtFormatter: NSDateFormatter = NSDateFormatter()
gmtFormatter.timeZone = NSTimeZone(name: "GMT")
gmtFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let gmtDate: NSDate = gmtFormatter.dateFromString(dateString)!
print("GMT \(gmtDate)")
let localFormatter: NSDateFormatter = NSDateFormatter()
localFormatter.timeZone = NSTimeZone.localTimeZone()
localFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let localDateString = localFormatter.stringFromDate(gmtDate) // <-- !!!
print("Local \(localDateString)")
Result:
GMT 2016-02-24 10:09:08 +0000
Local 2016-02-24 02:09:08
That's correct. When it's 10 AM in London, it's 2 AM here.
To put it another way, if you think what you're doing is converting a date from one time zone to another, you have not understood what a date is. It is a date-time and a time zone. There is nothing to convert: it is completely determined, and it is correct no matter where you are, because it says what time zone it is.
Perhaps it would be a bit clearer to you if I changed the next-to-last line format to this:
localFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
The final output is then:
Local 2016-02-24 02:09:08 -0800
which is obviously correct.

Related

Cannot convert to NSDate from this specific date (1994-04-01) String

Something strange happen to me.
I'm not able to convert this specific date (1994-04-01) String.
Can anyone check this and let me know if it reproduce in your code?
Steps to Reproduce:
Swift:-
let dateString = "1994-04-01"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateFromString = dateFormatter.date(from: dateString)
Obj c:-
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *birthdayStr = #"1994-04-01";
NSDate *birthday = [formatter dateFromString:birthdayStr];
Expected Results:
birthday = 1994-03-31 21:00:00 +0000
In UTC
Actual Results:
birthday = nil
Version:
Xcode ver :- Version 8.1 (8B62)
OS X ver :- 10.12.1 (16B2555)
you can try any different date then (1994-04-01) and it will work fine.
Test code:
import Foundation
for timeZoneId in TimeZone.knownTimeZoneIdentifiers {
let dateString = "1994-04-01"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: timeZoneId)
dateFormatter.dateFormat = "yyyy-MM-dd"
if dateFormatter.date(from: dateString) == nil {
print(timeZoneId)
}
}
Output:
Asia/Amman
Asia/Damascus
Asia/Gaza
Asia/Hebron
Asia/Jerusalem
I conclude your system time zone is set to one of the ones printed. If I type “jerusalem time zone 1994” into Google, the first result tells me that Daylight Saving Time started at midnight on April 1 in 1994. This means there was no midnight. The first instant of April 1, 1994 was in fact 1 AM in that time zone.
A DateFormatter uses a time of day of midnight by default when not parsing a time of day from the string. This makes it fail when midnight doesn't exist on the date in the string.
The solution is to not use midnight as your default time of day. Noon is a much safer default time. So, one solution is to include the time of day in the input and parse it in the format:
let dateString = "1994-04-01 12:00:00"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: timeZoneId)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
Another solution is to give the date formatter a default date that is noon of some day:
// Reference date was 00:00:00 UTC on 1 January 2001. This is noon of the same day in UTC.
dateFormatter.defaultDate = Date(timeIntervalSinceReferenceDate: 12*60*60)
If you are going to do any date parsing or manipulation on iOS or macOS, it would be a very good idea to watch WWDC 2013 Session 227: Solutions to Common Date and Time Challenges.

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 UTC NSDate into Local NSDate not working

I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
I convert this string into NSDate by this formate.
class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString(dateStr)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)
let dateForm = NSDateFormatter()
dateForm.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateForm.timeZone = NSTimeZone.localTimeZone()
let dateObj = dateForm.dateFromString(timeStamp)
return dateObj!
}
Suppose the parameter string is "2016-11-05T12:00:00.000Z" but when i convert this string and return a NSDate object it doesn't change the time according to my local time. I get my correct time in the timeStamp string (in above code). But when i try to convert that timeStamp string into NSDate it again shows that date and time which i got as a parameter.
You shouldnt change a NSDate's time. NSDates are just a point in time, counted by seconds. They have no clue about timezones, days, month, years, hours, minutes, seconds,… If printed directly they will always output the time in UTC.
If you change the date to show you the time of your timezone you are actually altering the time in UTC — hence your date becomes representing another point in time, no matter of the timezone.
Keep them intact by not altering them, instead when you need to display them do it via a date formatter.
If you need to do time calculations that are independent of timezones you also can work with NSDateComponents instead.
NSDate doesn't have a timezone. It's a point in time, independent of anything, especially timezones. You cannot "convert a UTC NSDate to a local NSDate", the statement itself doesn't make any sense.

NSDateFormatter decreases the day of date

I need to store Date variable in CoreData in iOS
I need to store the Date only without the Time, So I made a formatter that discard the time partition from the NSDate variable.
But I have a strange result:
This is my code:
let dateStr = "2016-02-14 11:27:01"
let df2 = NSDateFormatter()
df2.timeZone = NSTimeZone.defaultTimeZone()
df2.dateFormat = "yyyy-MM-dd HH:mm:ss"
print(dateStr)
if let date = df2.dateFromString(dateStr) {
df2.dateFormat = "yyyy-MM-dd"
print("-> \(df2.dateFromString(df2.stringFromDate(date)))")
}
and this is the output:
2016-02-14 11:27:01
-> Optional(2016-02-13 20:00:00 +0000)
Why does the formatter decrease the day by one ?
I tried many dates with same issue
Your time zone is obviously UTC+4.
To get UTC set the time zone accordingly.
df2.timeZone = NSTimeZone(forSecondsFromGMT: 0)
But although you see a date 4 hours ago the NSDate object is treated correctly depending on your time zone. The print command displays always UTC ignoring the time zone information, because NSDate is just a wrapper for a Double number.

Convert NSDate to String with a specific timezone in SWIFT

In my coredatabase I have an "news" entity with a NSDate attribute. My app is worldwide.
News was published 2015-09-04 22:15:54 +0000 French hour (GMT +2)
To save the date, I convert it, in UTC format.
let mydateFormatter = NSDateFormatter()
mydateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00"
mydateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
var dateToSaved : NSDate! = mydateFormatter.dateFromString(date)
The news is recorded with the date : 2015-09-04 20:15:54 +0000 (UTC)
Later in the app, I need to convert this NSDate saved in String:
let mydateFormatter = NSDateFormatter()
mydateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00"
var dateInString:String = mydateFormatter.stringFromDate(date)
Everything works perfectly if I launch the app in the same timezone when I published the news i.e GMT+2.
If I change the timezone of my device, for example UTC-4, then convert my NSDate in String, the date is not the same. I got : 2015-09-04 16:15:54 +0000
How to obtain the same UTC date as in my database for any timezone?
I'm not very comfortable with timezone, but I think my issue comes from the "+0000" in the NSDate. In all my dates, there is always +0000, it should be the right timezone, I think. But I don't know how to do.
Xcode 8 beta • Swift 3.0
Your ISO8601 date format is basic hms without Z. You need to use "xxxx" for "+0000".
"+0000" means UTC time.
let dateString = "2015-09-04 22:15:54 +0000"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(calendarIdentifier: .ISO8601)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss xxxx"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
if let dateToBeSaved = dateFormatter.date(from: dateString) {
print(dateToBeSaved) // "2015-09-04 22:15:54 +0000"
}
If you need some reference to create your date format you can use this:

Resources