问题
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 ?
回答1:
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
回答2:
Try This-
import UIKit
let date = NSDate();
// "Apr 1, 2015, 8:53 AM" <-- local without seconds
var formatter = NSDateFormatter();
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ";
let defaultTimeZoneStr = formatter.stringFromDate(date);
// "2015-04-01 08:52:00 -0400" <-- same date, local, but with seconds
formatter.timeZone = NSTimeZone(abbreviation: "UTC");
let utcTimeZoneStr = formatter.stringFromDate(date);
// "2015-04-01 12:52:00 +0000" <-- same date, now in UTC
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"]
来源:https://stackoverflow.com/questions/40169205/how-to-get-current-est-time-ios-swift