List of all American holidays as NSDates
- Here you'll find RSS-feeds with the holidays. It doesn't list year, but in the docs you'll find information how to change date ranges.
- Download it. I would suggest ASIHTTPRequest for that task.
parse the RSS-feed. you can do so by normal XML-parsing, or you use a specialized parser. MWFeedParser would be one option. - Save the dates. either by using CoreData, or Event Kit Framework
GMT timezone conversion in objective c
I think I can see what the problem is here - it's this line:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];
GMT-07:00
is not a valid abbreviation for this method. If you call [NSTimeZone abbreviationDictionary]
you'll get a dictionary containing all available abbreviations. You need to use the actual abbreviation (eg, 'PST' for Pacific Standard Time), and not the format "GMT-07:00".
To save you time, here's the full list of supported abbreviations using NSTimeZone
.
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
find EST date now in objective-c iOS
You say the time is reported as 2014-11-21T01:25:00
. That's a typical RFC 3339/ISO 8601 date. See Apple's Technical Q&A QA1480 for information on how to parse, which is usually:
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = enUSPOSIXLocale;
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [formatter dateFromString:timeStamp];
Note, in this format, the timestamp is almost always in GMT
, not EST
. To remove this ambiguity, most dates strings we get from servers are generally bear a Z
at the end, e.g. 2014-11-21T01:25:00Z
to unambiguously declare that the date in "Zulu", i.e. GMT
/UTC
. Even in the absence of timezone information, we'd generally expect this to still be GMT
. If you know for a fact that it's really EST
(which I highly doubt; that would be very unusual), then change the timeZone
line above (using your timeZoneWithAbbreviation
).
That's how you convert the date string you received from the server into a NSDate
. You can then compare that to [NSDate date]
as contemplated in your isEndDate:isSmallerThanCurrent:
routine. But do not try to convert a NSDate
into a different timezone.
You say currentESTDate
returns the incorrect time. The NSDateFormatter
will calculate the current date and time in EST in the currentDateStamp
variable. But you then convert it back, just retrieving the original NSDate
value. Thus this routine is unnecessary.
To be clear, NSDate
objects do not have timezones. They represent the same moment of time across the entire world. The time zones only come into play when you convert the date into a string (via NSDateFormatter
).
Thus, the problem is not "how do I convert [NSDate date]
into a particular timezone." The question is how one converts the 2014-11-21T01:25:00
string from the server into a valid NSDate
object (which can then be compared to other NSDate
objects). And hopefully the above code answers that question.
iPhone:Find the current timezone offset in HOURS
NSDate *sourceDate = [NSDate dateWithTimeIntervalSinceNow:3600 * 24 * 60];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
float timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate] / 3600.0;
NSLog(@"sourceDate=%@ timeZoneOffset=%d", sourceDate, timeZoneOffset);
Check if today is somebody's birthday
If your birthday is Feb 29, what logic do intend to use to answer the question "is today your birthday on a non leap year?". That's a non obvious thing to answer. The best I can come up is to consider Feb 28 your birthday, unless this year is a leap year, in which case Feb 29 is your birthday.
With that you can simply ask for how many days are in February this year and then do some simple logic to pretend the birthday is actually the 28th in a non leap year.
// Psuedo code
var bday = someUser.bday
if (bday.month == 2 && bday.date == 29 && daysInFebruraryThisYear == 28) {
bday.date = 28
}
// Is it today?
return bday.month == today.month && bday.date == today.date
NSTimeZone timeZoneWithName: works not properly
My current time (at that moment) is: 3:00 PM
New York's time (at that moment) is: 8:00 AM
Solution:
NSDate *date = activityDate; // Server sends me the date: 2013-09-10 09:00:00 +0000
NSString *dateFormat = @"dd MMM, yyyy HH:mm";
//Then the transformation comes here
NSDateFormatter* newYorkDf = [[NSDateFormatter alloc] init];
[newYorkDf setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[newYorkDf setDateFormat:dateFormat];
NSDateFormatter* gmtDf = [[NSDateFormatter alloc] init];
[gmtDf setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[gmtDf setDateFormat:dateFormat];
NSDate *gmtDate = [gmtDf dateFromString:[newYorkDf stringFromDate:date]];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:dateFormat];
NSString *stringDate1 = [format stringFromDate:gmtDate];
self.labelDate.text = stringDate1; // Returns me: 10 Sept, 2013 8:00
Evernote search with date range
In Evernote your dates are being kept in UTC.
When you make the search you need to create an Evernote search grammar that's relative to the timezone that you're interested in. In your case the timezone of the client or of the iPhone.
To get the user timezone:
ENSession * session = [ENSession sharedSession];
EDAMUser * user = session.user;
where the EDAMUser class has this structure:
@interface EDAMUser : FATObject
@property (nonatomic, strong) NSNumber * id; // EDAMUserID
@property (nonatomic, strong) NSString * username;
...
@property (nonatomic, strong) NSString * timezone;
For example my user timezone is: America/Montreal so on EST.
In order to get all the notes created May 11th you need to construct this Evernote search grammar:
@"created:20150511T040000Z -created:20150512T040000Z"
notice the ...T040000Z at the end.
So the conclusion is that you need to include the "definition" of the date from the client's perspective otherwise the query will work on UTC.
Here is an example of how to build the Evernote grammar search for the current day:
-(NSString *)buildQueryStringForDate: (NSDate *)date {
NSDateFormatter * formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyyMMdd'T'HHmmss'Z'"];
formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
formatter.locale = [NSLocale systemLocale];
DateRange * dateRange = [DateRange rangeForDayContainingDate:[NSDate new]];
return [NSString stringWithFormat:@"created:%@ -created:%@", [formatter stringFromDate:dateRange.startDate], [formatter stringFromDate:dateRange.endDate]];
}
The code for [DateRange rangeForDayContainingDate:[NSDate new]] can be found here: How can I generate convenient date ranges based on a given NSDate?
I hope this helps.
Related Topics
3 Component Dynamic Multi UIpickerview Swift
Optional Protocol Requirements, I Can't Get It to Work
How to Grab The Parent Object from a Subview
Support Arkit in Lower End Devices
Adding Drop Shadow to Table View Cell
Querying Geohashes in Firestore Returns Nothing
Macos App Sandboxing - Read Access to Referenced Files from Parsed Xml
Passing Data Between View Controllers (Swift)
Calling C++ from Swift - What Is The Equivalent of Std::Vector<T>
Leaks in Navigationview/List/Foreach with Dynamically Generated Views
I Opened My App in Xcode 10 and Now I Have Errors in 9.4.1: Sdkapplicationdelegate (Facebookcore)
Problems with Unified Logging, Staticstring, Customstringconvertible and Description
How to Use Nscalendar Range Function Within Calendar
Why Do I Get Rgb Values of (0,0,0) for an Nsimage with a Transparent Background
Way to Check If Up or Down Button Is Pressed with Nsstepper
Could Not Find an Overload for "Init" That Accepts The Supplied Arguments (Swift)
Swift Unsafemutablepointer & Unsafemutablepointer<Unsafepointer<Sometype>>