I need to compare two dates in my app and i have a EST date to compare with current date , but let today = NSDate()
returns the date in UTC , how can i get current EST time or convert to EST ?

- 208
- 5
- 19
-
Are you looking to compare the dates all the way up to the hour or are you looking to compare the dates simply up to the day? – Aaron Oct 21 '16 at 05:46
-
1Can you show your EST date? IS it in string format or NSDate? – Nirav D Oct 21 '16 at 06:33
3 Answers
The NSDate
store the time in absolute moment i.e. No matter in what time zone your date was created when you use compare
method of NSDate
it will respect the time zone (because time zone was never stored). You can think of NSDate
as epoch time which is always calculated from 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970
I don't know about the implementation of NSDate
may be it store the date in UTC
internally.
It's our implementation (NSDateFormatter
) that changes it's format to our requirement.
So whether you use compare
or NSCalendar
regardless how you created your date they will compare you date
e.g. My local time zone is IST and it's 2016-10-21 12:10:00 here right now
In EST it's 2016-10-21 02:40:00,
In PST it's 2016-10-20 23:40:00 right now
So this code
let today = NSDate()
print("\(today)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(abbreviation: "EST")
let estDate = formatter.date(from: "2016-10-21 02:40:00")
print("\(estDate!)")
formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: "2016-10-20 23:40:00")
print("\(pstDate!)")
formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: "2016-10-21 12:10:00")
print("\(istDate!)")
Will print the same time because it's the same moment everywhere
2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000

- 39,458
- 17
- 135
- 184
-
7I just wanted to add a caveat that that getting the date for the "EST" timezone will fail if the current east coast timezone is in "EDT" for daylight savings. It is better to use formatter.timeZone = TimeZone(identifier: "America/New_York") which respects both EDT and EST. – Adam Freeman May 22 '19 at 18:46
Try This-
import UIKit
let date = Date();
let formatter = DateFormatter();
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
let defaultTimeZoneStr = formatter.string(from: date)
formatter.timeZone = TimeZone(abbreviation: "UTC")
let utcTimeZoneStr = formatter.string(from: date)
If you need to compare two dates, you can use the method below
// Date comparision to compare current date and end date.
var dateComparisionResult:NSComparisonResult = NSDate().compare(endDate)
if dateComparisionResult == NSComparisonResult.OrderedAscending
{
// Current date is smaller than end date.
}
else if dateComparisionResult == NSComparisonResult.OrderedDescending
{
// Current date is greater than end date.
}
else if dateComparisionResult == NSComparisonResult.OrderedSame
{
// Current date and end date are same.
}
If you want to know all time zones abbreviations available you can do like this:
var timeZoneAbbreviationsType: [String:String] { return TimeZone.abbreviationDictionary }
timeZoneAbbreviationsType // ["CEST": "Europe/Paris", "WEST": "Europe/Lisbon", "CDT": "America/Chicago", "EET": "Europe/Istanbul", "BRST": "America/Sao_Paulo", "EEST": "Europe/Istanbul", "CET": "Europe/Paris", "MSD": "Europe/Moscow", "MST": "America/Denver", "KST": "Asia/Seoul", "PET": "America/Lima", "NZDT": "Pacific/Auckland", "CLT": "America/Santiago", "HST": "Pacific/Honolulu", "MDT": "America/Denver", "NZST": "Pacific/Auckland", "COT": "America/Bogota", "CST": "America/Chicago", "SGT": "Asia/Singapore", "CAT": "Africa/Harare", "BRT": "America/Sao_Paulo", "WET": "Europe/Lisbon", "IST": "Asia/Calcutta", "HKT": "Asia/Hong_Kong", "GST": "Asia/Dubai", "EDT": "America/New_York", "WIT": "Asia/Jakarta", "UTC": "UTC", "JST": "Asia/Tokyo", "IRST": "Asia/Tehran", "PHT": "Asia/Manila", "AKDT": "America/Juneau", "BST": "Europe/London", "PST": "America/Los_Angeles", "ART": "America/Argentina/Buenos_Aires", "PDT": "America/Los_Angeles", "WAT": "Africa/Lagos", "EST": "America/New_York", "BDT": "Asia/Dhaka", "CLST": "America/Santiago", "AKST": "America/Juneau", "ADT": "America/Halifax", "AST": "America/Halifax", "PKT": "Asia/Karachi", "GMT": "GMT", "ICT": "Asia/Bangkok", "MSK": "Europe/Moscow", "EAT": "Africa/Addis_Ababa"]

- 385
- 1
- 11

- 2,339
- 2
- 24
- 38
The time zone problem is because when you print Date() the value you get is of UTC zone. (GMT + 00:00).
Now to convert this into your local date do the following steps.
- Know the difference in time between your zone and the UTC time. If you are not sure, just google it and you'll get the time difference.
Eg: I am from India and the time difference is 5 hrs 30 mins. IST is GMT + 05:30.
Add that time interval to the standard UTC date.
extension Date { static func getCurrentDate() -> Date { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy" let curr = Date().addingTimeInterval(TimeInterval((5*3600) + (30*60))) return curr } }
Here, 5 * 3600 is for the 5 hrs, and 30*60 is for the 30 min difference. So, I am basically converting the 5hrs 30 mins in seconds and adding that time interval.
It worked for me. Hope you find this useful too!

- 31
- 1
- 9